At least 40 others were wounded in the attack that began late on Friday night and security forces rescued many others, including children, from the scene at Mogadishu’s popular Hayat Hotel, they said on Saturday.
The attack started with explosions outside the hotel before gunmen entered the building.
The extremists blasted the hotel with two car bombs before opening fire but by Saturday afternoon police said all the attackers had been killed. Witnesses said the army used heavy weapons, pummelling the building to finish off the besieged terrorists.
"The security forces have ended the siege now and the gunmen are dead," an army official told AFP on condition of anonymity. "We've had no incoming gunfire from the building in the past hour."
Most of the civilians who were in the hotel at the time of the gun and bomb assault on Friday evening were rescued.
"It took a long time because of the complexity of the rescue mission," AFP cited police officer Ibrahim Duale as saying.
Somalia's Al Shabab insurgents claimed responsibility, Reuters reported.
Mohamed Abdirahman, director of Mogadishu’s Madina Hospital, told the Associated Press that 40 people were admitted after the attack. While nine were sent home after receiving treatment, five remained in critical condition in the intensive car unit, he said.
Abdullahi Hussein said he and others were having tea near the hotel lobby when they heard the first blast, followed by gunfire.
"I immediately rushed towards hotel rooms on the ground floor and I locked the door,” he said by phone.
“The militants went straight upstairs and started shooting. I was inside the room until the security forces arrived and rescued me.”
He said on his way to safety he saw “several bodies lying on the ground outside the hotel reception”.
Al Shabab, which has been waging an insurgency against Somalia's central government for about 15 years, said it was in control of the hotel on Saturday.
The blasts sent huge plumes of smoke over the busy junction on Friday night and the sound of gunfire crackled across Mogadishu on Saturday.
Journalist Harun Maruf tweeted footage of the explosion, although the death toll had risen since his tweet, which put it at "at least 15".
Government forces tried to wrest control of the hotel from the militants, witnesses said.
They reported at least two large explosions as the gunmen attacked the hotel, a spot frequented by government officials and ordinary Somalis in a bustling area on the airport road.
Police spokesman Abdifatah Adan Hassan told reporters late on Friday that the initial explosion was caused by a suicide bomber who had forced his way into the hotel accompanied by several gunmen.
Witnesses said a second blast occurred a few minutes later, inflicting casualties on rescuers and members of the security forces and civilians who rushed to the scene after the first explosion.
The militants claimed responsibility in a brief statement on an Al Shabab propaganda website, saying its fighters were carrying out “random shooting” inside the hotel.
Al Shabab spokesman Abdiaziz Abu Musab told the group's Andalus radio on Saturday that its forces were still in control of the building and that they had “inflicted heavy casualties”.
Dozens of people gathered outside the four-storey hotel to discover the fate of friends, relatives and colleagues, AFP reported.
Friday's bombing was the first major attack since President Hassan Sheikh Mohamud took office in May.
Al Shabab claimed responsibility for the attack, according to a translation by the Site Intelligence Group, which monitors extremist group statements.
Al Shabab said it wants to establish its rule of Somalia based on a strict interpretation of Islamic law.
The Hayat is popular with politicians and other government officials. There was no information on whether any of them had been caught up in the siege.
The attack drew widespread international condemnation on Sunday. The UN released a statement saying Secretary General Antonio Guterres expressed "his deep condolences to the families of the victims, as well as the government and people of Somalia".
The Muslim World League also condemned the attack, rejecting all forms of extremism and giving "full solidarity with dear Somalia in its war against terrorism and against all that may threaten its security and stability".
In Washington, the State Department condemned the attack and said the US remained "steadfast in our support of Somali and African Union-led efforts to counter terrorism and build a secure and prosperous future for the people of Somalia".
In the Middle East, the UAE gave "sincere condolences and sympathy to the government and people of the Federal Republic of Somalia and to the families of the victims of this heinous crime".
Secretary General of the Gulf Co-operation Council, Nayef Al Hajraf, also wished a speedy recovery for the wounded, saying the bloc was against "against all forms of violence, extremism and terrorism".