Somalia's special forces killed all three armed men who had taken over a building in the center of Mogadishu, using civilians as human shields.
The attack, which killed 20 people, began Thursday when Al-Shabab fighters stormed an area of hotels, shops and restaurants.
They then grabbed a nearby building.
This was the longest siege of the militant group since it was expelled from the Somali capital in 2011.
The last shooter was killed on Friday night about 22 hours after the first attack.
Ismail Muktar, Commissar of the Hamar Jajab district, told reporters about the last moments of the siege.
"The last armed terrorist was killed after the security forces destroyed a room where he was hiding and the siege is over," he said.
Al-Shabab said it was behind the attack in the bustling street of Maka al-Mukarama, which also injured more than 60 people.
A number of civilians were rescued from the building.
There are fears that the death toll could continue to rise.
What led to the siege?
An al-Shabab fighter blew himself up in a car and destroyed other cars on the street and in front of the Maka al-Mukarama Hotel, witnesses say.
Mohamed Moalimuu, Secretary-General of the National Association of Somali Journalists (NUSOJ), was with a colleague at the Hotel Maka al-Mukarama when she heard shots, followed by an explosion:
The suicide bomb blast was followed by shots from other al-Shabab fighters storming an adjacent building.
US-trained Somali troops, known as the Alpha Group, were called upon 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 finished those lessons, but we were worried [about] the civilians they are [al-Shabab] It does not matter to use shields, but we would rather be the shields for them [the civilians]Said Dahir Mahamud Gelle in a televised address.
What is the meaning of the street Maka al-Mukarama?
Maka al-Mukarama Street is the busiest street in Mogadishu. It has been repeatedly targeted by al-Shabab fighters, despite being one of the country's most guarded roads.
Hassan Haile, a prominent Somali political analyst based in the UK, told the BBC that the Islamist militant group was particularly interested in the street.
"Al-Shabab likes to attack Maka al-Mukarama because it is in the heart of Somalia," he said.
He told the BBC that the Islamist militant group used 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 get where they want to go," Haile said.
"There is negligence on the part of the government, the soldiers have no money and they receive no encouragement to make such attacks possible."
The fight against al-Shabab
The Islamist group al-Shabab, which is linked to al-Qaeda, was expelled from Mogadishu in 2011, but continues to carry out regular attacks in the city.
The US has heavily increased its air strikes against the group since President Trump took power.
Former Deputy Director of the National Intelligence and Security Agency (NISA) in Somalia says Al-Shabab has intensified its attacks in Mogadishu in retaliation for the air strikes.
Abdisalam Guled was also concerned that the withdrawal of Burundian troops from the African Union troop in that city could further endanger security this week.
He also believes that bad management is partly to blame.
"Army and police are overworked and underpaid," he said.
The US State Department says al-Shabab retains control of large parts of the country and can conduct high-profile attacks with suicide bombers, detonators, mortars and handguns.