Security forces ended the siege by the al-Shabab attackers at the Sahafi Hotel by midday, said police commander Ali Ahmed.

"It's over now, we have killed all the attackers," he said. "They came under cover of darkness and attacked the hotel while some of the guards were sleeping."

The attack started at daybreak when a suicide bomber detonated a vehicle laden with explosives at the gate of the Sahafi Hotel and then men armed with AK-47 rifles, propelled grenades and suicide vests invaded the hotel.

Some of them went into rooms to kill residents while others went to the rooftop to fight government soldiers who came to fight the attackers, said Captain Mohamed Hussein, a Somali police officer.

A second explosion came from a car bomb outside the hotel, said witnesses. Among those killed was the hotel's owner and a former military general, Mr Hussein said.

"Had it not been the courage of some of the hotel residents who fought back the terrorists, the death toll could have been a lot higher," Mr Hussein said.

"They came in firing bullets randomly and chanting 'God is great' -they shot anyone they could see," said a surviving hotel resident, Ahmed Abdulle.

"The guards tried to fight them off but it was too late, they were already inside. I hid myself under my bed until security forces broke into my room and got me out a back door. It was terrible."

Al-Shabab, the Islamic extremist rebels waging an insurgency against Somalia's weak UN- backed government, claimed responsibility for the attack in a statement by Sheikh Abdiaziz Abu-Musab, the insurgents' military spokesman.

Somali troops and African Union forces went to the scene and fought the attackers to take control of the hotel, according to a Twitter post by the African Union Mission in Somalia.

One photographer was among those killed, and another was injured.

The Sahafi Hotel is often frequented by Somali government officials and business executives and has been targeted before.

Two French security advisers were abducted from the hotel by militants in 2009.