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 five people, police and the Islamist rebels said.
It is the latest in a series of raids in Mogadishu by the group which wants to topple the Western-backed government and impose its own strict version of Islamic law.
The group also attacked a university campus in neighbouring 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.
He was speaking as fighting still raged around the building that houses the Higher Education Ministry and the Petroleum and Minerals Ministry. 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 the group was behind the attack.
Police Colonel Hussein Ibrahim said at least five people had been killed, including an African Union and a government soldier. The other three were civilians. In addition, he said two al Shabaab gunmen who had stormed inside were killed.
"AU forces were always inside the building for protection, otherwise the situation would be worse. Now the fighting is over and the building is secured," Ibrahim said.
The AU has been supporting Somali troops in a campaign against al Shabaab. The government relies heavily on the African peacekeepers for security, guarding major buildings and installations.
Trader Omar Mohamed, who works near the scene, said he was thrown off his chair when the blasts went off. He said attackers sprayed gunfire at security forces in the fight 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 even 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.