Four Islamist gunmen were killed after detonating a car bomb and shooting their way into a national intelligence agency training site, the internal security ministry said, adding that the government did not suffer any casualties during the attack.
A spokesman for Al-Shabaab, which wants to topple a Western-backed government in Mogadishu, said the group's fighters had killed more than 10 intelligence officials.
In the past, Al-Shabaab has exaggerated the number of government members it has killed, while officials have played down losses.
The interior ministry displayed the insurgents' bullet-ridden bodies to the media following the assault, which started just after dawn and lasted for about one hour.
"We foiled the attack. As you can see, these are the dead bodies of the four Al-Shabaab men who attacked this building," Mohamed Yusuf, internal security ministry spokesman, told reporters at the scene.
"Two were in the car bomb while the other two were infantry. There were no other casualties," he added.
Sheikh Abdiasis Abu Musab, Al-Shabaab's military operations spokesman, told Reuters the militants lost three fighters.
"They martyred themselves in the operation. We killed over 10 intelligence (officials), mostly officers," he said.
The Al-Qaeda-aligned group has in the past stepped up attacks during the Muslim fasting month of Ramadan, which began on Wednesday. Al-Shabaab fighters on Saturday attacked a police post near the capital, killing eight officers.
The latest assault highlights Al-Shabaab's ability to launch attacks even though it has lost territory and urban strongholds since a major military offensive against them began last year.