The president was visiting the Walter Reed military hospital outside Washington, where he met wounded soldiers and health care workers.
"I've never been against masks but I do believe they have a time and a place," he said as he left the White House.
He has previously said that he would not wear a mask and mocked Democratic rival Joe Biden for doing so.
But on Saturday he said: "I think when you're in a hospital, especially in that particular setting, where you're talking to a lot of soldiers and people that, in some cases, just got off the operating tables, I think it's a great thing to wear a mask."
The change of tone came as the US recorded 66,528 coronavirus cases on Saturday, a new daily record.
What has Trump said about face masks?
Speaking to Fox Business Network last week, Mr Trump said: "I'm all for masks."
He added that he "sort of liked" how he looked with one on, likening himself to the Lone Ranger, a fictional masked hero who with his Native American friend, Tonto, fought outlaws in the American Old West.
But when the US Centers for Disease Control (CDC) in April began recommending people wear masks or cloth coverings in public to help stop the spread of the virus, Mr Trump told reporters he would not follow the practice.
"I don't think I'm going to be doing it," he said back then. "Wearing a face mask as I greet presidents, prime ministers, dictators, kings, queens - I just don't see it."
Some media reports have suggested aides have repeatedly asked the president to wear one in public.
In an interview with the Wall Street Journal last month, Mr Trump suggested some people might wear masks to signal disapproval of him.
He also said he took issue with people touching their faces after taking their mask off.
"They put their finger on the mask, and they take them off, and then they start touching their eyes and touching their nose and their mouth. And then they don't know how they caught it?" he said.
What's the current situation in the US?
The US has seen another 66,528 infections in the past 24 hours, a record for one day, and a total of almost 135,000 deaths since the pandemic began, according to Johns Hopkins University data.
Louisiana has become the latest state to order that masks be worn in public.
Democratic Governor John Bel Edwards also ordered the closure of bars across Louisiana, and tightened restrictions on restaurants, which will no longer be able to serve customers inside. The measures come into effect on Monday.
State Republican lawmakers are expected to oppose the move.
"If you don't like the mask mandate, then don't like it while you wear your mask," Governor Edwards said. "If you want to be mad at me about it, then be mad at me about it."
Neighbouring Texas has recorded another rise of coronavirus infections, with a record 10,500 new cases recorded on Saturday.
The governor of South Carolina has issued an order banning sales of alcohol after 23:00 in bars and restaurants to try to stop the spread of the virus.
A court in Indiana has halted the execution of a convicted killer as the victim's relatives said they were worried about travelling during the pandemic to watch him die. Daniel Lee was set to be executed on Monday in what would have been the first federal execution in 17 years. BBC