An attack on the intelligence compound in Somali capital Mogadishu by al-Shabaab militants has reportedly killed several security personnel.

Security forces have claimed to have thwarted the attack though the fatalities have been reported.

"We foiled the attack. As you can see, these are the bodies of the four al-Shabaab men who attacked this building," Mohamed Yusuf, spokesperson for the interior security ministry, told reporters parading the Islamists' corpses.

The insurgent group has claimed responsibility for the raid.

"We captured the building and we are fighting inside it," Sheikh Abdiasis Abu Musab, a spokesperson for the group, told Reuters.

Gunfire was reported shortly after a huge blast.

The latest attack has taken place a day after a similar raid in the nearby town of Afgoi, which is believed to have killed eight police officers.