A nine-man suicide commando blasted its way into Mogadishu's main court complex Sunday, some blowing up their explosives vests while others sprayed gunfire in a rampage that left 29 civilians dead, while a separate bomb attack killed five more.
The Al-Qaeda-linked Shebab militant group claimed responsibility for the courthouse attack, which the country's chief justice escaped unharmed on the worst day of violence the city has seen in months.
"The African Union force, the Somalia national army and local police have cleared the building. The latest figures indicate that there are 29 killed civilians, nine dead Shebab militants and 58 people injured," a security officer told AFP on condition of anonymity.
Interior Minister Abdikarin Hussein Guled said all the attackers were killed in the courthouse raid. "Six detonated themselves, and three were shot dead by security forces," he told reporters, adding that the government had regained control of the situation.
Shebab spokesman Sheikh Ali Mohamed Rage claimed only five suicide attackers died in the fighting at the court complex, and vowed there would be fresh attacks.
"This was a holy action which targeted non-believers who were in a meeting within the court complex. We will continue until Somalia is liberated from invaders," he told AFP.
The Islamist Shebab militants used to control most of the seaside capital until it abandoned fixed positions in August 2011 but it has since carried out a string of attacks against the UN-backed government.
After Somali forces eventually ended the raid, during which a car bomb also exploded, chaos still engulfed the area and an AFP reporter saw medics evacuating wounded through the courthouse's shattered windows.
"Some of the terrorists are still inside the court building and they are wearing explosive vests," Fadumo Ali, a civilian trapped inside the building, told AFP during the raid from a mobile phone.
As the drama inside the court complex was unfolding, five people were killed when a remote-detonated car bomb near the airport struck a convoy carrying Turkish aid.
"Five people, two of them women who were passing by the area were killed in the car bomb attack," Hassan Moalim, who witnessed the attack, told AFP.
He said two of the bodies were charred beyond recognition.
An official from the Turkish Red Crescent confirmed to Turkish news television channel NTV that the organisation suffered casualties.
"We unfortunately lost our Somali driver in the blast," said Ahmet Lutfi Akar, the head of the Turkish Red Crescent. "Three Turks were also mildly injured but they are in hospital now and doing well."
Turkey has recently taken a leading role in Somalia and is very active in reconstruction and aid projects across the country, which has been left in ruins by two decades of almost uninterrupted conflict.
A regional military offensive has forced many Shebab fighters to retreat to the mountains in northern Somalia but the insurgents have reverted to guerrilla tactics and carried out several bomb attacks in Mogadishu.
"This attack is nothing but a sign of desperation by the terrorists, who've lost all their strongholds and are in complete decline right across Somalia," Somalia's President Hassan Sheikh Mohamud said in a statement.
The courthouse is in the heavily-guarded administrative quarter of the capital and several senior officials were caught in the chaos.
"The chief justice and other senior judiciary officials are all safe and sound, they were rescued unharmed but unfortunately a number of civilians and security personnel were killed in the attack," police commander Mohamed Yusuf said.
Among the dead from the courthouse attack were two prominent lawyers, Mohamed Mohamud Afrah and Abdikarin Hassan, who recently defended a journalist who stood trial after interviewing a rape victim.