(CNN) -- Militants with the Islamist group al-Shabaab stormed Somalia's presidential palace in Mogadishu on Tuesday, firing their way onto the grounds after a car bomb blasted the gate, authorities said.
Somali and African Union troops exchanged heavy gunfire with the militants and thwarted the attack, a senior Somali security official said.
President Hassan Sheikh Mohamud was not harmed and Somalia's government praised the troops.
"Fortunately, the prompt actions of the Somali National Army, assisted by AMISOM forces (African Union Mission in Somalia), quickly halted the attack," said government spokesman Mustafa Duhulow.
Three attackers were killed and one was captured, he said.
"Bomb disposal personnel have rendered safe a number of devices, including a suicide vest that one attacker was wearing that had failed to detonate," Dhulow said.
Al-Shabaab said it was behind the raid, one of several recent Mogadishu attacks for which the al Qaeda-linked group has claimed responsibility as it presses its campaign to turn Somalia into a fundamentalist Islamic state.
Earlier, al-Shabaab spokesman Abdulaziz Abu Musab claimed the group's militants had captured the offices of the President and the prime minister.
"Terrorists can make as many claims and threats on the internet as they want," Mohamud said from his office. "The attack failed."
U.S. condemns attack
The U.S. State Department issued a statement condemning the attack and praising AMISOM troops, which get U.S. support.
"Incidents such as this further demonstrate that insecurity in Mogadishu persists, including very real threats from al-Shabaab," the statement said.
The incident comes after several high-profile attacks in the capital, including a car bombing -- claimed by al-Shabaab -- that killed at least six people at the Parliament building on Saturday.
On Thursday, gunmen killed one Somali lawmaker and wounded another in a drive-by shooting in Mogadishu, the third assassination of a Parliament member claimed by al-Shabaab this year.
Al-Shabaab, designated a foreign terrorist organization by the United States, has a relationship with al Qaeda that goes back several years. The two groups effectively merged in 2012, according to CNN national security analyst Peter Bergen.
The group aims to establish Islamic rule in Somalia, though it has carried out attacks in other African countries, as well. It allegedly carried out a mall attack in Nairobi, Kenya, in September that left at 67 people dead.
CNN's Nana Karikari-apau contributed to this story