Somali government forces have ended a more than five hourslong al-Shabab siege of the compound housing the Mogadishu mayor's office and other local government facilities.
The Ministry of Information, Culture and Tourism said security forces shot dead six al-Shabab fighters behind Sunday's attack.
Five civilians were killed and four others were injured, the statement added.
The Somali government said the al-Shabab attackers arrived at the compound on foot, masquerading as government soldiers.
The attack started with an explosion followed by the gunmen storming the building.
"At about 12 noon they hit the back gate facing Hamarweyne (district) with an explosion," Deputy Mayor Isse Gure said.
Gure told VOA all the top local government officials with the exception of Mayor Yusuf Hussein Jimale were on-site when the attack occurred. Jimale is currently out of the country.
Gure also said troops were alert and minimized casualties.
Gure said the security forces rescued officials and staffers. He confirmed a local government staffer with special needs was among the five civilians who lost their lives.
Through the Telegram messaging app, al-Shabab claimed responsibility for the attack. Al-Shabab's complex attacks usually involve an initial explosion followed by suicide bombers storming their target. The government refers to al-Shabab militants as Khawarij, meaning 'deviants.'
In a statement, the group claimed the deaths of 34 people including officials, administrative workers, soldiers and officers. The claim has not been independently verified.
"When the explosion happened, I was in Hamarweyne, not that far from where the explosion took place," said VOA Somali stringer Abdiaziz Ahmed Barrow.
He said people were preparing for midday prayers at the time.
"We heard a huge explosion, it was a powerful one that shocked the people," he said. "I saw people fleeing from the scene; the ambulances came to the scene. The security forces have arrived, and they have opened fire."
Barrow said people started running as the situation changed quickly.
"People I know were injured [in the attack], some of them [are] my friends," he said. "I have seen some people with blood running away."
The mayor's office was targeted by an earlier al-Shabab attack, July 24, 2019, when a female suicide bomber fatally wounded then-Mayor Abdirahman Omar Osman.
The latest incident comes as the Somali government continues its military operations in several regions to dislodge al-Shabab from towns and villages. On Saturday, officials in Somalia's Jubaland state announced that troops had freed the town of Jana Cabdalle, 60 kilometers (37 miles) west of Kismayo, in a new multi-front offensive in the southern part of the country.
The day before, al-Shabab stormed a Somali government military base in the newly captured town of Galcad in Galmudug state. Al-Shabab claimed the deaths of 159 soldiers as the Somali government acknowledged that seven soldiers were killed. Among them was Major Hassan Osman Mohamed, known as Hassan Tuure, a commander with the Danab (lightning) forces trained by the United States.
The U.S. military reported that 30 al-Shabab fighters were killed in a "collective self-defense strike" against al-Shabab near Galcad on January 20. U.S. Africa Command, or AFRICOM, said the strike followed al-Shabab's attack on Somali government forces in Galcad.
The Somali government said al-Shabab's "barbaric" attack on Sunday is further proof that the militants need to be eliminated from the country.
Asha Ibrahim Aden and Mohamed Abdurahman contributed to this report.