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-Shabaab 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-Shabaab 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.
An Al-Shabaab 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 with a colleague when they heard gunshots followed by a blast:
The suicide car explosion was then followed by gunshots from other Al-Shabaab 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-Shabaab had prolonged the siege.
"We could have ended this hours ago but we were concerned [about] the civilians, whom they [Al-Shabaab] 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.
The Maka al-Mukarama road is the busiest road in Mogadishu. It has been repeatedly targeted by al-Shabaab 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-Shabaab 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 Islamist group Al-Shabaab, 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-Shabaab 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-Shabaab 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.