Somalia's Al-Qaeda-linked Shebab militants on Tuesday blasted their way into the higher education ministry with a car bomb before storming the building, killing 15 people.
Police and witnesses said the car bomb caused a huge explosion which allowed the gunmen to force their way into the fortified building.
Ambulance officials said at least 15 people were killed and 20 wounded in the attack.
Six Shebab gunmen were also killed, with two of those blowing themselves up, internal security ministry spokesman Mohamed Yusuf Osman said.
Abdukadir Abdirahman Adan, head of Mogadishu's Ambulance Services, told reporters his crews had taken about 15 dead bodies and 20 wounded civilians to hospital.
Troops backed by African Union soldiers regained control of the building after around an hour-long attack, which began when "a car loaded with explosives rammed the gate," police officer Mohamed Dahir said
"The security forces and AU peacekeepers shot and killed four of the attackers, while the other two blew up themselves," Osman told reporters.
Witness Idil Malim said bodies of those killed and wounded lay around the complex.
"There were several people lying dead at the scene," he said.
Shebab spokesman Abdulaziz Abu Musab claimed responsiblity, boasting that their gunmen had been in "fully in control" of the ministry, as well as entering a neighbouring building housing the oil ministry.
The building is in the K5 district of the capital, which has been hit by a string of similar attacks in recent months.
The Shebab rebels stage regular attacks in the capital as part of their fight against the country's internationally-backed government and African Union forces supporting it.
Complex attack
A car bombing to force entry into fortified buildings followed by an armed raid has become a trademark tactic of the hardline Islamists.
Somalia has been unstable since the collapse of Siad Barre's hardline regime in 1991, and the country's new government is being supported by a 22,000-strong African Union force that includes troops from Burundi, Djibouti, Ethiopia, Kenya and Uganda.
Shebab fighters have carried out a string of revenge attacks in neighbouring countries, notably Kenya and Uganda, in response to their participation in the AU force.
It carried out its deadliest attack yet earlier this month, when Shebab gunmen massacred 148 people in a day-long siege of a university in Kenya's northeastern town of Garissa.
The attack on the university was the Shebab's bloodiest massacre and Kenya's worst since the 1998 bombing of the US embassy in Nairobi.
The Shebab later warned of a "long, gruesome war" unless Kenya withdraws its troops from Somalia, as well as warning the government in Mogadishu it would continue to attack them on home soil.
Shebab fighters also carried out the Westgate shopping mall attack in Nairobi in September 2013, a four-day siege which left at least 67 people dead.
Last month Shehab gunmen in Mogadishu stormed the fortified Maka al Mukarama hotel, used by politicians, diplomats and businessmen.
Despite losing significant territory in recent months the Shebab group, whose name means "youth" in Arabic, still manages to launch frequent attacks as part of its fight to overthrow the government.
Last week the government in Mogadishu issued bounties for 11 top leaders of the Shebab, with $250,000 offered for the extremist's chief, Ahmad Umar.
Track Latest News Live on NDTV.com and get news updates from India and around the world