The Islamist group detonated car bombs before launching nighttime raids at two locations in southern Somalia.
The Somali military repelled twin attacks by al Qaeda-linked al-Shabab early on Saturday, inflicting "heavy losses," the army chief said.
Addressing reporters, General Odowa Yusuf Rage said that "thanks to our brave soldiers who knew about the tricks of the assailants, the militants were defeated and their wounded and dead bodies are strewn around." He added that the military was still pursuing the attackers.
What do we know about the attacks?
The attackers used car bombs before trying to storm the military bases located some 100 kilometers (60 miles) from the capital, Mogadishu.
Various military officials gave differing death tolls, with the military initially saying that 19 militants were killed in both locations.
Later, a senior military official told media that the soldiers killed 45 jihadists during the attacks. Several of the soldiers were also killed.
What do witnesses say?
In Awdheegle, where the larger of the two bases is located, witnesses said the attackers failed to breach the facility despite nearly an hour of fighting.
"I saw several dead bodies of the Shabab gunmen near the camp where the fighting occurred, the Somali soldiers paraded these bodies after the fighting," town resident Mohamed Ali told the AFP news agency.
In Bariire, witnesses said the militants managed to enter the camp and torch some of the military supplies.
Separately, the groups said it has captured military vehicles and supplies in the attacks.
What is al-Shabab?
Al-Shabab was founded between 2004 and 2006, and claims to fight for an Islamic theocracy in the Horn of Africa. They are believed to have ties to other major terror groups such as al Qaeda and Boko Haram.
While an African Union-led offensive pushed them out of Mogadishu in 2011, they have continued to launch bombing attacks and raids in Somalia and Kenya.
dj/mm (AFP, dpa, AP)