Somali government and African Union (AU) forces have taken full control of the last port city held by militant Islamists, officials say.
Heavy gunfire could be heard as the AU and Somali forces entered Barawe, 220km (135 miles) south-west of the capital Mogadishu, on Monday, residents said.
The al-Qaeda-linked al-Shabab group surrendered the town without much resistance.
The AU says al-Shabab used Barawe as a base to launch attacks on the capital.
Somalia’s central government last controlled Barawe 23 years ago.
Tanks and armoured vehicles patrolled the town as a military commander addressed hundreds of residents, calling on them to support the government.
“Al-Shabab are no longer in Barawe,” said the commander, Abdirisak Khalif Elmi, AFP news agency reports.
Residents said many of the al-Qaeda-aligned militants began withdrawing from the port town on Friday.
Al-Shabab has lost control of several towns in the past month, but still controls large swathes of territory in rural areas.
The BBC’s Emmanuel Igunza says the fall of Barawe is a significant blow to al-Shabab because they had used the town as a supply route for weapons and food and as a base for a lucrative charcoal business.
The loss of Barawe – after six years under their control – comes a month after al-Shabab’s leader, Ahmed Abdi Godane, was killed by a US air strike near the town. US strikes have also targeted other senior militants in and around Barawe.
The group, which is estimated to have at least 5,000 fighters, wants to overthrow the UN-backed Somali government and has imposed a strict version of Sharia in areas under its control.
Last week, a woman was stoned to death in Barawe for alleged adultery.
Correspondents say al-Shabab tends to tactically withdraw from areas when faced with a large offensive, but leaves some fighters within the civilian population to launch attacks later.