The assault on the Gofgadud base in the Bay region by the al-Qaida-affiliated extremist group marks the latest setback for Somalia's army, which is expected to take over responsibility for the country's security from an African Union force next year.
Col. Ahmed Yusuf, a Somali military officer, told The Associated Press that Somali troops made a brief tactical withdrawal amid heavy artillery shelling before regaining control of the base. He said six al-Shabab fighters were killed in the army's counter-attack that forced the extremists to withdraw.
Al-Shabab claimed responsibility for the attack, asserting that it killed or wounded more than 30 soldiers.
Years of conflict and al-Shabab attacks, along with famine, shattered Somalia, which is home to more than 12 million people.
The Horn of Africa nation has been trying to rebuild since establishing its first functioning transitional government in 2012. Al-Shabab was pushed out of the capital, Mogadishu, and other major cities several years ago but still carries out suicide attacks across Somalia.
With a federal government established, pressure is growing on Somalia's military to assume full responsibility for the country's security.