Zakia Hussein, deputy commissioner of the Somali Police Force, said security forces ended a seven-hour siege on popular SYL Hotel after killing all the five attackers.
"The attack was concluded and the five attackers were killed by the security forces," Hussein said in a statement, adding that two soldiers and three civilians were killed in the attack.
She said two soldiers and nine civilians sustained injuries in the attack on the hotel, which sits in a heavily guarded vicinity near the presidential palace.
Hussein said the security forces safely evacuated 82 people trapped inside the hotel, including some prominent figures. The hotel is frequented by government officials and high-profile business people.
Unconfirmed reports say a judge was among those killed in the attack.
Sources said guards and security forces thwarted attempts by the militants to gain entry into the hotel. Witnesses recounted the attack earlier to Xinhua, saying they heard a powerful blast followed by gunfire.
The Al-Shabab terror group, which has been trying to overthrow the government, claimed responsibility for the attack.
The group, which has repeatedly staged similar attacks on government buildings and officials, said it attacked the hotel because it is close to the presidential palace.
More about: Somali