Al Shabaab Islamist militants attacked a government building housing two ministries in the Somali capital on Tuesday, setting off two big blasts before gunmen stormed inside, killing at least 10 people, police and the rebels said.
Fighting raged around the building, which houses the ministries for higher education and petroleum and minerals, before security forces retook it from the attackers.
It is the latest in a series of raids in Mogadishu by al Shabaab, which wants to topple Somalia's Western-backed government and impose its own strict version of Islamic law.
The group also attacked a university campus in neighboring Kenya this month, killing 148 people.
"First two blasts occurred, a bike blast and a car blast, outside the building, then armed fighters stormed in," Major Ali Nur, a police officer, told Reuters.
About an hour and a half after the explosions, police said they had secured the building.
Al Shabaab's military operations spokesman Sheikh Abdiasis Abu confirmed that the group was behind the attack.
Government spokesman Ridwaan Haji said on his official Twitter account that 10 people had been killed in the attack, including two soldiers and eight civilians.
He said seven militants had also been killed.
Abdikadir Abdirahman, director of Mogadishu ambulances, told Reuters at least 15 civilians had been killed and 20 others injured.
He did not give details of any soldiers or militants killed.
Al Shabaab said only three of its fighters were killed and that 20 others, including soldiers and ministry workers, had died.
The death toll cited by the group often differs from official ones.
Police Colonel Hussein Ibrahim said one of the soldiers was with the African Union peacekeeping force, which has led a military campaign against al Shabaab with Somali troops and also guards major government buildings and installations.
"Now the fighting is over and the building is secured," Ibrahim said.
The government spokesman could not immediately confirm the death of the AU soldier.
Trader Omar Mohamed, who works near the scene, said he was thrown off his chair by the blasts.
He said attackers sprayed gunfire at security forces during the battle to retake the building.
Al Shabaab, which once ruled much of Somalia, lost control of Mogadishu in 2011 and has been driven out of remaining major strongholds in a joint AU-Somalia military offensive launched last year.
Despite losing swathes of territory, the group has repeatedly shown it can strike Somali targets and also across the border into Kenya.
Earlier this month, al Shabaab fighters attacked a university campus in the Kenyan town of Garissa, which lies about 200 km (120 miles) from the border, killing 148 people.
The group has said it wants to punish Kenya for sending troops to Somalia as part of the AU force.
At the end of March, al Shabaab militants attacked a hotel in the Somali capital, killing 14.