Nine al-Shabab militants wearing military fatigues and carrying guns and grenades died after attacking Somalia's presidential palace with two car bombs.
The president called the assault in Mogadishu a "media spectacular" by a "dying animal".
President Hassan Sheikh Mohamud was unharmed, but two government officials were killed, the interior ministry said.
A speeding car full of explosives rammed into a barricade erected by soldiers protecting the presidential palace, causing an explosion and sending plumes of smoke into the sky.
Amid the mayhem, gunmen chanting "God is great" then moved toward a second gate and tried to force their way into the complex.
The attack highlights a worrying new trend in Mogadishu, that despite a period of relative calm following al-Shabab being ousted from the city in August 2011, militants have carried out a series of deadly assaults in recent weeks that have seen the city hit with mortar fire and pitched battles.
Weapons meant for the Somali army could have been used by the militants in yesterday's attack. A confidential UN Monitoring Group on Somalia and Eritrea reported this month that the country's military is selling weaponry in markets where the al Qaida-linked militants buy weapons.
In at least one case weapons were sold by a military commander directly to an al-Shabab commander, the confidential report said.
Yesterday's attack against the compound where the president and prime minister live began with a car bomb explosion, followed by an assault by gunmen on palace guards, said police Captain Mohamed Hussein.
"President just called me to say he's unharmed. Attack on Villa #Somalia had failed. Sadly some lives lost. I condemn strongly this terrorism," the UN representative to Somalia, Nick Kay, said on Twitter.
He added later: "The Somali people are tired of shootings, bombings and killings. It's time for a new chapter in Somalia's history."
The interior ministry displayed the seven bloodied and dead bodies of the attackers and said two others blew themselves up. The wreckages of two car bombs lay nearby.
The two others killed included a former intelligence commander and an aide to the prime minister, a Somali-American named Mohamud Hersi Abdulle.
"Apart from media headlines, #Shabaab will achieve nothing from it," a Twitter account run by the office of the president said.
"Don't be fooled by this 'media spectacular'. This is another act of desperation from a dying animal."
Al-Shabab has been waging war in Somalia for years as it tries to oust a Western-backed government. Weakened from its apex of power, the militants are still able to launch vicious attacks.
UN Secretary-General Ban Ki-moon condemned the attack "in the strongest terms" while the UN Security Council said it was "appalled". Both paid tribute to Somali and African Union forces for repelling the attack.
The Security Council reaffirmed "that this and other acts of terrorism would not weaken their determination to support the peace and reconciliation process in Somalia".
The secretary-general expressed concern that recent attacks by al-Shabab "are clearly aimed at destabilising the country at a time when many efforts are being mobilised to restore peace and development," UN deputy spokesman Farhan Haq said.
The UN Monitoring Group report, published on February 6 and obtained by The Associated Press, found that many weapons given to Somalia's military can no longer be accounted for, including rocket-propelled grenades, hand grenades and bullets.
The Monitoring Group "has developed serious concerns that the 1,000 AK-47s delivered from Uganda" are no longer in government control, it said.