Al-Shabab says its fighters killed 27 soldiers in the raid, about 50km (31 miles) from the port city of Kismayo.
A government spokesman confirmed to BBC Somali that the militants had seized the base, before soldiers retook it after heavy fighting. Six soldiers and 87 militants were killed, he said.
Al-Shabab has been waging an insurgency in Somalia for more than 10 years.
A 22,000-strong African Union force is in the country to help the government fight the al-Qaeda-linked militants.
The US military also has a presence of several hundred troops in Somalia to carry out special operations and to train government forces.
Some Somali government soldiers "fled into the jungles" following the attack on the base in Baar Sanguni, an al-Shabab spokesman, Abdiasis Abu Musab, said.
The sound of the blast and shooting was so loud that residents in the town of Jamame, about 70km from Kismayo, said they heard it, Reuters news agency reports.
"We heard a huge blast after early morning prayers. Then heavy exchange of gunfire followed. It was in the direction of Baar Sanguni," resident Osman Abdullahi was quoted as saying.
Government spokesman Aden Isaq Ali told BBC Somali the attack was repelled after the military sent reinforcements.
Troops were pursuing the militants, and had taken control of a key bridge, he added.
Al-Shabab did not say how many fighters it lost in the battle.
The militant group carried out an assault in the same area last month.
At the time, it said its fighters had killed a US soldier, two Kenyans and nine Somali troops.
Al-Shabab has been driven out of many urban areas since 2011, however it still controls territory in rural areas and carries out gun attacks and bombings on military and civilian targets.
A truck attack in the capital, Mogadishu, on 14 October 2017 killed at least 500 people, in the deadliest bombing in Somalia.
A man accused of leading an al-Shabab unit which carried out the attack was sentenced to death in February.