MOGADISHU: Security forces have ended a siege by al Qaeda-linked militants at a hotel in Somalia's capital in which more than 20 people were killed and dozens injured, authorities said on Sunday.

Dozens of people who had been taken hostage in the stand-off at the Hayat Hotel in Mogadishu were freed.

Elite armed forces battled the militants for 30 hours from Friday evening after the attackers blasted and shot their way into the hotel, which is popular with lawmakers and other government officials. 

"We have confirmed so far 21 dead people and 117 others injured," Ali Haji, the minister for health, told national broadcaster SNTV.

"It is possible there were corpses that were not taken to hospitals but buried by relatives. The death toll and the casualties are based on the figure taken to hospitals," the minister added.

Three attackers were shot dead during the military operation to end the siege, said Hassan, a police captain who only gave one name.

A fourth one was shot dead in the area on Sunday morning as he attempted to melt into the civilian population, Hassan said, adding that the exact number of attackers was still unclear.

The al Qaeda-linked al Shabaab group, which has been fighting to topple the government in the Horn of Africa country for more than a decade, claimed responsibility for the attack.

It wants to establish its own rule based on a strict interpretation of Islamic law.

Friday's attack at the Hayat was the first major incident since President Hassan Sheikh Mohamud took office in May.

The attackers shot and killed civilians who fled towards the wall of the hotel compound and its blown-up gate after they struck, said a police captain who gave only the name Ahmed, adding that they killed 10 security personnel with firearms and grenades.

Aden Ali, a survivor, told Reuters that he was drinking a cup of tea at the hotel when he heard the first blast. He ran towards the compound wall with others as the militants fired at them.

"We were many on the run, over a dozen. When I went out of the hotel, I could see eight of us. Maybe the rest died in the shooting," Ali said.

Another group of people in the hotel fled to an upper floor, where they were killed by the militants who first blew up the stairs to prevent escape, Ali said.

Security forces managed to free some of those who locked themselves in their rooms in the upper floors after several hours, he said.

Some 106 people were freed, including women and children, a senior police commander said.

Residents were milling around the bombed out hotel on Sunday. The building was extensively damaged.

"We are still investigating the explosions of many plastic bags that have been scattered around the hotel," said Mohamed Ali, a military officer at the scene.