Secret Service agents spotted and fired on a gunman in bushes near the property line of the golf course, a few hundred yards from where Trump was playing, law enforcement officials said.
The suspect left an AK-47-style assault rifle and other items at the scene and fled in a vehicle but was later arrested.
Both incidents highlight the challenges of keeping presidential candidates safe in a hotly contested and polarised campaign with just over seven weeks to go before the November 5 election.
It was not clear if or how the suspect knew Trump was playing golf at the time, but the attempted attack was sure to raise new questions about the level of protection he is given.
CNN, Fox News and The New York Times identified the suspect as Ryan Wesley Routh, 58, of Hawaii, citing unnamed law enforcement officials. The FBI declined to comment.
Reuters found profiles on X, Facebook, and LinkedIn for a Ryan Routh who appeared to be the man identified as the suspect by those news organisations.
Reuters was not able to confirm these were the suspect's accounts and law enforcement agencies declined to comment, but public access to the Facebook and X profiles was removed hours after the shooting.
The three accounts bearing Routh's name suggest he was an avid supporter of Ukraine in its war against Russia. In several of the posts, he appeared to be trying to help recruit soldiers for Ukraine's war effort.
GUN BARREL IN BUSHES
Palm Beach County Sheriff Ric Bradshaw said Secret Service agents saw a rifle barrel poking out from bushes about 400 to 500 yards (365 to 460 metres) away from Trump as they cleared holes of potential threats ahead of his play.
The agents engaged the gunman, firing at least four rounds of ammunition around 1.30pm on Sunday (local time).
The gunman then dropped his rifle, and left behind two backpacks and other items, and fled in a black Nissan car. The sheriff said a witness saw the gunman and managed to take photos of his car and licence plate before he escaped.
"The Secret Service did exactly what should have been done," Bradshaw said, declining to identify the suspect or provide a possible motive.
After the suspect fled the scene, police sent out an alert to statewide agencies with the information on his vehicle, which led to sheriff’s deputies in neighbouring Martin County apprehending the suspect on I-95 about 65km from the golf course.
Fox News presenter Sean Hannity said he'd spoken to both Trump and Steve Witkoff, a New York real estate investor and longtime Trump friend who was on the golf course with him on Sunday.
"They were on the fifth hole. And the way Steve described this, the way the president described it, they both had exactly the same story, which is that they heard pop pop, pop pop," said Hannity. The Secret Service "pounced on the president, covered him", he added.
In response to a reporter’s question, officials acknowledged that because Donald Trump is not in office, the full golf course was not cordoned off.
"If he was, we would have had the entire golf course surrounded,” Bradshaw said during Sunday's briefing. “Because he’s not, security is limited to the areas that the Secret Service deems possible.”
Trump sent an email to supporters saying there were "gunshots in my vicinity, but before rumors start spiraling out of control, I wanted you to hear this first: I AM SAFE AND WELL!" according to an email seen by Reuters.
The White House said in a statement that President Joe Biden and Vice President Kamala Harris had been briefed about the incident and were relieved to know that he was safe.
Biden later said he had directed his team to ensure the Secret Service has the resources it needs to ensure Trump's safety, according to a statement released by the White House.
Trump is locked in a tight presidential election race with Harris, who has had a surge in the polls since replacing Biden as the Democratic Party's candidate in July.
"Violence has no place in America," Harris said in an X social media post.
On X in 2020, Routh expressed support for Democratic US presidential candidate Bernie Sanders and mocked Biden as "sleepy Joe."
Earlier this year, Routh tagged Biden in a post on X: "@POTUS Your campaign should be called something like KADAF. Keep America democratic and free. Trumps should be MASA ...make Americans slaves again master. DEMOCRACY is on the ballot and we cannot lose."
Trump's running mate in the presidential election, US Senator JD Vance, said he spoke to Trump after the shooting and that the former president was in good spirits.
Trump was grazed in the right ear and one rallygoer was killed in the gunfire at the Pennsylvania rally on July 13. The gunman, identified as a 20-year-old Thomas Crooks, was shot and killed by a Secret Service sniper.
That was the first shooting of a US president or major party presidential candidate in more than four decades, and the glaring security lapse forced Kimberly Cheatle to resign as Secret Service director under bipartisan congressional pressure.
The Secret Service's new acting director said in August that he was "ashamed" of the security lapse that led to the assassination attempt.