The president, who has spent much of the year downplaying the threat of a virus that has killed more than 205,000 Americans, said he and Mrs Trump were quarantining.
The White House physician said the president is expected to continue carrying out his duties “without disruption” while recovering. A White House official said Mr Trump was experiencing mild symptoms but was working from the White House residence.
Senate majority leader Mitch McConnell said he had spoken with Mr Trump on the phone and he was in “good spirits”.
The diagnosis is sure to have a destabilising effect in Washington and around the world, raising questions about how far the virus has spread through the highest levels of the US government.
Hours before Mr Trump announced he had contracted the virus, the White House said a senior aide who had travelled with him during the week had tested positive.
“Tonight, @FLOTUS and I tested positive for COVID-19. We will begin our quarantine and recovery process immediately,” Mr Trump tweeted. “We will get through this TOGETHER!”
Vice president Mike Pence tested negative for the virus on Friday morning and “remains in good health”, his spokesman said.
Many White House and senior administration officials are undergoing tests, but the full scale of the outbreak around the president may not be known for some time as it can take days for an infection to be detectable by a test.
Mr Trump was last seen by reporters returning to the White House on Thursday evening and did not appear visibly ill. He is 74, putting him at higher risk of serious complications from a virus that has infected more than seven million people nationwide.
The president’s physician said in a memo that Mr Trump and the first lady, who is 50, “are both well at this time” and “plan to remain at home within the White House during their convalescence”.
The diagnosis is a devastating blow for a president who has been trying desperately to convince the American public that the worst of the pandemic is behind them.
In the best of cases, if he develops no symptoms it is likely to force him off the campaign trail weeks before the election and puts in doubt his participation in the second presidential debate, scheduled for October 15.
His handling of the pandemic has already been a major flashpoint in his race against Democrat Joe Biden, who spent much of the summer off the campaign trail and at his home in Delaware because of the virus.
Mr Biden has since resumed a more active campaign schedule, but with small, socially distanced crowds. He also regularly wears a mask in public, something Mr Trump mocked him for at Tuesday night’s debate.
“I don’t wear masks like him,” the president said. “Every time you see him, he’s got a mask. He could be speaking 200ft away from me, and he shows up with the biggest mask I’ve ever seen.”
In a tweet on Friday morning, Mr Biden said he and his wife “send our thoughts to President Trump and First Lady Melania Trump for a swift recovery. We will continue to pray for the health and safety of the president and his family”.
Mr Biden and his running mate Kamala Harris have tested negative, their campaign said.
Mr Pence tested negative on Friday morning and “remains in good health,” his spokesman said.
Doug and I join Joe Biden and Dr. Biden in wishing President Trump and the First Lady a full and speedy recovery. We’re keeping them and the entire Trump family in our thoughts.
— Kamala Harris (@KamalaHarris) October 2, 2020
It was not immediately clear whether the former vice president had been tested since appearing at the debate with Mr Trump or whether he was taking any additional safety protocols. The two men did not shake hands during the debate, but stood without masks about 10ft apart for the 90-minute event.
On Friday, Mr Trump had been scheduled to receive an intelligence briefing, attend a fundraiser and hold another campaign rally in Florida, but the White House released a revised schedule with only one event: a phone call on “Covid-19 support to vulnerable seniors”.
Mr Trump’s announcement came hours after he confirmed that Hope Hicks, one of his most trusted and longest-serving aides, had been diagnosed with the virus on Thursday.
She began feeling mild symptoms during the plane ride home from a rally in Minnesota on Wednesday evening, according to an administration official, and was isolated from other passengers aboard the plane.
Ms Hicks had been with Mr Trump and other senior staff aboard Marine One and Air Force One en route to that rally and had accompanied the president to Tuesday’s presidential debate in Cleveland, along with members of the Trump family. The Trump contingent removed their masks during the debate, in violation of the venue rules.
Multiple White House staff have previously tested positive for the virus, including Mr Pence’s press secretary Katie Miller, national security adviser Robert O’Brien and one of the president’s personal valets.
But Mr Trump has consistently played down concerns about being personally vulnerable, even after White House staff and allies were exposed.
Since coronavirus emerged earlier this year, he has refused to abide by basic public health guidelines — including those issued by his own administration — such as wearing face coverings in public and practicing social distancing. Instead, he continued to hold campaign rallies that drew thousands of often mask-less supporters.
“I felt no vulnerability whatsoever,” he told reporters in May.