Al Qaeda-linked group used guns and car bombs to take control of the base. The attack comes three weeks after an assault on the Kismayu army base near the border with Kenya.
Al Shabab fighters killed at least 15 soldiers in an attack on a military base near Somalia's capital, Mogadishu, the militant group and Somali officials said on Friday.
The militants used guns and car bombs to take control of the base and a nearby town, Barire, 50 km southwest of Mogadishu, in an early morning attack.
The fighters also took 11 pickup trucks mounted with machine guns, known as "technicals," said Abdiasis Abu Musab, al Shabab’s military operation spokesman.
A Somali military officer put the number of dead at 15; Al Shabab said 21 were killed.
The government and Al Shabab often give differing accounts of death tolls.
"There was a battalion of soldiers but it was a fierce fighting; twin suicide car bombs and hand-to-hand fighting," Captain Osman Ali said.
"The attacked soldiers were Somali military and the special forces trained by the US called Danab. There were neither AMISOM nor other foreigners there," Ali added, referring to African Union peacekeeping troops.
Reinforcements were being sent, he added.
Al Shabab aims to topple the government in Mogadishu and impose its own strict interpretation of Islam. Somalia has been at war since 1991, when clan-based warlords overthrew dictator Siad Barre and then turned on each other.
The Al Qaeda-linked group was driven out of the capital in 2011, but it still carries out frequent attacks on security and government targets and African Union peacekeeping troops.
Civilians sometimes get caught in the crossfire.
Ali Nur, the deputy governor of Lower Shabelle region where Barire is situated, confirmed the fighting. So did local residents.
"First we heard two huge blasts at the base and then heavy exchange of gunfire followed. Now it looks like the fighting died down," Ali Farah said.
Two weeks ago, Al Shabab attacked an army base at a town near the border with Kenya. Three weeks ago they struck another in the southern port city of Kismayu, killing a total of at least 43 people in the two attacks.