Somalia's special forces have killed all three gunmen who had taken over a building in central Mogadishu, using civilians as human shields, officials say.
The attack, which killed 20 people, began late on Thursday when al-Shabab militants stormed an area lined with hotels, shops and restaurants.
They then seized a nearby building.
This was the longest siege by the militant group since it was forced out of the Somali capital in 2011.
The final gunman was killed on Friday evening, about 22 hours after the initial attack.
Ismail Muktar, the commissioner for Hamar Jajab district, told reporters about the final moments of the siege.
"The last terrorist gunman was killed after the security forces destroyed a room in which he was taking cover and the siege is over now," he said.
Al-Shabab has said it was behind the attack in the busy Maka al-Mukarama road, which has also injured more than 60.
A number of civilians were rescued from the building.
There are fears that the death toll could rise further.
What led to the siege?
An al-Shabab fighter blew himself up in a car, destroying other cars on the road and the front of Hotel Maka al-Mukarama, witnesses say.
The secretary-general of the National Union of Somali Journalists (NUSOJ) Mohamed Moalimuu was inside the Hotel Maka al-Mukarama with a colleague when they heard gunshots followed by a blast:
We were so lucky to survive the huge blast this evening in #Mogadishu. I and my colleague HM Dahir were sitting down in Maka Almukarama hotel .We first heard gunfire followed by a very demolishing blast The scene changed into darkness.Heavy smoke went high. More bodies were scene pic.twitter.com/JXxn9NdwBK— Mohamed Moalimuu (@MOALIMUU) February 28, 2019
The suicide car explosion was then followed by gunshots from other al-Shabab militants who stormed an adjacent building.
US-trained Somali troops known as the Alpha Group were called in to surround the building and end the siege.
Somalia's information minister said on Friday afternoon that the use of human shields by al-Shabab had prolonged the siege.
"We could have ended this hours ago but we were concerned [about] the civilians, whom they [al-Shabab] don't mind using as shields but we would rather be the shields for them [the civilians]," Dahir Mahamud Gelle said in a televised speech.
What is the significance of Maka al-Mukarama road?
The Maka al-Mukarama road is the busiest road in Mogadishu. It has been repeatedly targeted by al-Shabab militants despite being one of the most heavily guarded roads in the country.
Hassan Haile, a prominent UK-based Somali political analyst, told the BBC that the Islamist militant group were especially drawn to the road.
"Al-Shabab like attacking Maka al-Mukarama because it is in the heart of Somalia," he said.
He told the BBC the Islamist militant group use bribes and threats to carry out their attacks:
"They either bribe with money or make it very clear that they know where the soldiers live and who their families are, to reach where they want to reach," Mr Haile said.
"There is negligence from the government, the soldiers have no money and they don't get encouragement which makes these kinds of attacks possible."
The fight against al-Shabab
The Islamist group al-Shabab, which is linked to al-Qaeda, was forced out of Mogadishu in 2011 but continues to mount regular attacks in the city.
The US has sharply increased its air strikes against the group since President Trump took power.
The former deputy director of Somalia's National Intelligence and Security Agency (NISA) says that al-Shabab has increased its attacks in Mogadishu as retaliation for the airstrikes.
Abdisalam Guled was also concerned that this week's withdrawal of Burundian troops from the African Union force in the city could further jeopardise security.
He also believes that bad management is partly to blame.
"The army and police are overworked and underpaid," he said.
The US State Department says al-Shabab retains control over large parts of the country and has the ability to carry out high-profile attacks using suicide bombers, explosive devices, mortars and small arms.