People walk among rubbles following an attack outside a hotel on January 25, 2017, in Mogadishu. [File, AFP]

Al-Shabab launched a deadly attack on a Somali military base on Tuesday, just a day after the government claimed a "historic victory" over the jihadists with the capture of a strategic coastal town.

There were conflicting reports about the death toll after the Islamist fighters stormed the camp in the town of Hawadley north of the capital Mogadishu.

Army chief Odowaa Yusuf Rage said on national radio that five soldiers including a senior officer had died in the attack claimed by the al-Qaida-allied militant group.

A clan militia commander near Hawadley, which lies in the central Hirshabelle state, said 11 soldiers had died.

The Islamists detonated a car packed with explosives outside the camp 60 kilometers (37 miles) north of Mogadishu before gunmen stormed the compound, witnesses and security officials said.

Al-Shabab, which controls swathes of countryside in Somalia, claimed responsibility for the attack via its communication channels.

In recent months, the army and local clan militias have retaken chunks of territory from the militants in Galmudug and Hirshabelle states in an operation backed by US air strikes and an African Union force.

But despite the gains, al-Shabab has demonstrated the ability to strike back with lethal force against civilian and military targets.

The Hawadley base had only been recaptured from al-Shabab in October last year by the Somali National Army (SNA) and allied militias.

Prime Minister Hamza Abdi Barre on Monday proclaimed a "historic victory" over Al-Shabaab after the army took control of the strategic city of Haradhere in central Galmudug state without a fight.

"The brave members of the national armed forces... have destroyed the enemy of the nation and liberated the strategic port town of Haradhere," he said in a statement.

Haradhere had been a key supply route for Al-Shabaab for both people and goods after it seized the port in 2010, dislodging local militias and pirates.

'Attempt to distract'

Tuesday's attack "demonstrates al-Shabab's continued ability to produce explosive devices and deploy them within Hirshabelle state, where the offensive originally began", said International Crisis Group's senior analyst for eastern Africa, Omar Mahmood.

"The group has mounted a number of similar assaults recently, likely an attempt to distract the government by attacking multiple locations," he told AFP in a message.

On Saturday, eight people were killed in a roadside bombing claimed by al-Shabab in central Somalia, police said. Earlier this month, 19 people were killed in twin car bombings in Mahas, a town in Hiran district in Hirshabelle.

Rage said the army had repelled the assault in Hawadley and was pursuing the militants who got away.

"Five members of the army were martyred, including a senior military officer," he said, adding that the army had killed 21 al-Shabab fighters.

Ahmed Mohamud, an SNA military commander in the nearby town of Balcad, said more than 10 people had died, but added that it was not clear how many were soldiers and that the toll was provisional.

He said the army was "in full control" of the area.

President Hassan Sheikh Mohamud had declared "all-out war" on the jihadists after taking office in May last year.

Last week, he called on ordinary Somalis to help flush out members of the jihadist group he described as "bedbugs".

Although forced out of Mogadishu and other main urban centers more than a decade ago, al-Shabab remains entrenched in parts of rural central and southern Somalia.