Militants attacked Somalia's presidential compound on Tuesday with a car bomb and gunmen broke through a perimeter wall but were repulsed by security forces, and the president was not there at the time, the interior ministry said.
Up to five members of the al Shabaab Islamist group, which claimed responsibility, were killed, Interior Minister Abdullahi Godah Barre told Reuters. Three militants were confirmed dead, and one or two more were believed to have died in the car blast.
The assault was the most dramatic in a string of attacks in the capital Mogadishu by al Shabaab since it launched a campaign during the current Muslim fasting month of Ramadan.
But President Hassan Sheikh Mohamud was not there at the time as he was at another location attending an iftar, the meal to break the Ramadan fast after sunset.
"I can assure you the president is not hurt and as a matter of fact he was not in the palace," the minister said, adding teh gunmen were repulsed in the car park near the prime minister's offices and had not made it to the presidential quarters.
It is the second time since February that al Shabaab have attacked the sprawling compound, which includes the presidential buildings and other government offices. Officials said security had been tightened since then, including adding stronger gates.
Officials said the attackers were quickly defeated by Somali security forces and members of the African Union peacekeeping force, which remains the backbone for Somali state security.
However, al Shabaab said fighting still raged after officials said it was finished. "Let them enter the palace and meet us if the fighting is over," Sheikh Abdiasis Abu Musab, spokesman for al Shabaab's military operations, told Reuters.
He said their fighters had killed 14 soldiers in the raid, while the minister said there had been no such casualties. The minister also said details of the attack were still not clear and greater clarity would come in the morning.
African Union forces along with the Somali army launched a new offensive this year against al Shabaab, which is fighting to impose its strict interpretation of Islam on Somalia.
Several towns have been wrested back from the group but officials say the Islamists still control tracts of countryside and some settlements, from where they have been able to continue their guerrilla-style campaign.