The assault began when a suicide bomber rammed his explosive-laden car into the base in El Salini, 60 kilometers (37 miles) southwest of Mogadishu, at around 5:30 am local time, al Shabaab said.
A local elder, Ahmed Cali, told Reuters he had been woken by the sound of a huge blast followed by gunfire. He said that later al Shabaab fighters were seen leaving the base, some in trucks loaded with ammunition.
A major in the Somali military who didn't want to be named said the military has sustained some casualties, without giving details. He said the army had received reinforcements and was back in control of the base.
Al Shabaab claimed responsibility for the assault and said they had killed 23 soldiers.
"A mujahid driving a suicide car bomb first hit the base and then armed mujahideen stormed the base, we killed 23 military soldiers," said Abdiasis Abu Musab, Al Shabaab's military operations spokesman.
He added the fighters had taken military vehicles and weapons from the base, including anti-aircraft guns.
Al Shabaab is fighting to topple Somalia's central government which is defended by the African Union (AU)-mandated peacekeepers.
The militants were driven out of Mogadishu in 2011 and have since lost most of their other strongholds, but still carry out frequent attacks across Somalia. They have also orchestrated assaults outside of Somalia, mostly in neighboring Kenya.