Two suicide car bombers struck two army positions in the district of Awdhegle and Bariire village in the volatile Lower Shabelle province, dpa news agency reported.
"Al-Shabaab attackers carried out a complex attack on two military bases in Lower Shabelle province this morning," government spokesman Ismael Mukhtar said here.
Shortly after the bombings, the heavily-armed insurgents tried to storm the army bases.
Gunfire was exchanged until the morning, when the Somali armed forces said they had killed 19 militants in both attacks.
"We have repelled the terrorists who wanted to cause havoc on our army. We have lost 4 soldiers and scores of others were wounded," army commander Abdullahi Rage told dpa.
He said soldiers were pursuing the attackers and further information about casualty numbers would be provided later.
Residents also said civilians had been injured by stray bullets.
The Islamist group al-Shabaab claimed the attack through its Andalus radio station, saying numerous soldiers had been killed.
Al-Shabaab has been fighting for dominance in the Horn of Africa country for years.
The terrorist group controls large parts of the south and centre and repeatedly carries out attacks on civilians and security forces.