The siege in the heart of the Somali capital lasted almost 24 hours and left 35 people dead with 80 wounded. The assault began with bombings before the gunmen opened fire from inside buildings.
Somali security forces ended a siege by al-Shabab gunmen in Mogadishu on Friday nearly a day after the militants launched a deadly attack in the Somali capital, officials said.
"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," Hamar Jajab district commissioner Ismail Mukta said.
"The security forces are clearing the area."
The siege began on Thursday shortly after the Islamist militants detonated a bomb near the Maka Almukarramah hotel, which is frequented by government officials, killing at least 24 people and injuring dozens more.
Al-Shabab said a suicide bomber from the group was responsible for the attack, which it said aimed to kill senior officials staying at the hotel.
The hotel is situated in a busy street in Mogadishu that was filled with people at the time of the attack, witnesses said.
Al-Shabab, which retains control of large rural areas after being ousted from bases in Mogadishu in 2011, has been carrying out a long fight to dislodge a Western-backed government. The US under President Donald Trump has dramatically stepped up airstrikes on the group, with the US Africa Command saying it had killed 52 militants since February 23.
Al-Shabab has also carried out deadly attacks in neighboring countries that contribute to the African Union force supporting the government, including one in Kenya in January that killed 21 people.
Somalia has been a state of lawlessness and violence since 1991 when dictator Siad Barre was ousted by rebel groups.