Al Shabaab Islamist militants have rammed a car packed with explosives into a Somali army base southwest of the capital and stormed inside, killing at least 10 soldiers.
The assault on a base 50km outside Mogadishu on Monday (local time) was part of the group's campaign to try to topple the Western-backed government and impose its own strict interpretation of Islam.
"Heavy exchange of gunfire continued for hours," Major Ahmed Farah said from the nearby town of Afgooye.
At least 12 al Shabaab fighters were killed, he added.
Al Shabaab spokesman Abdiasis Abu Musab said 30 soldiers had been killed in the raid that lasted several hours before his group's withdrew.
He did not mention al Shabaab casualties.
The group often cites a higher death toll than officials.
On Sunday, the Somali president's office said in a statement the government destroyed an al Shabaab base used to store military supplies in a region south of Mogadishu without resistance.