Police spokesman Sadik Dodishe said nine others, including five soldiers, were wounded in the suicide bombing at the military facility in the Elasha Biyaha area of the Lower Shabelle region.
The Islamic extremist group al-Shabab claimed responsibility. The group frequently carries out such attacks.
Photos shared on the social media platform X showed the extent of the damage caused by the explosion, which destroyed the security base and several civilian houses in the vicinity.
"The sound of the explosion was deafening ... and I saw a plume of smoke going up in the sky," witness Ayan Hussein, a resident of Elasha Biyaha, said by phone. She said she saw several injured people being carried into an ambulance.
Somalia's federal government has battled al-Shabab for years. Security forces stepped up their assault on the group in recent months, including trying to dismantle its propaganda network.