Somali survivors have described harrowing scenes of the night-long siege of a popular Mogadishu restaurant by al-Shabaab militants who set off a car bomb, and then shot their victims, killing 31 and wounding nearly 40 others.

'I never thought I would have the chance to see the sun again.' - Saida Hussein, siege survivor

After security forces ended the siege, survivors were led by soldiers from the Pizza House restaurant building. The injured were taken by ambulances.

Al-Shabaab claimed responsibility as the restaurant was under siege.

Soldiers surrounded the restaurant building and used guns mounted on the backs of vehicles to neutralize the militants. Troops entered the ground floor while the insurgent snipers held positions upstairs.

5 attackers killed

All five attackers were killed and after dawn the soldiers secured the building, said senior Somali police office Capt. Mohamed Hussein. The troops' efforts to take control of the Pizza House restaurant were slowed by the darkness of night, forcing them to wait until morning, said Hussein.

Somalia Restaurant Blast

Somali men carry away the body of a civilian who was killed in the attack. (Farah Abdi Warsameh/Associated Press)

Survivors recounted harrowing stories of hiding under tables, as armed attackers continued firing in the restaurant and hunted for patrons. Attackers moved from room to room, looking for people, said a survivor.

Capt. Mohamed Hussein said many of the victims were killed at point-blank range after the attackers hunted them down.

Somalia Restaurant Blast

Somali survivors described harrowing scenes of the night-long siege, which was ended by security forces. (Farah Abdi Warsameh/Associated Press)

"I never thought I would have the chance to see the sun again. They were killing people on sight," Saida Hussein, a university student, told The Associated Press. She said she survived the attack by hiding behind a large table downstairs.

Another survivor, Aden Karie, was wounded by an attacker who spotted him moving behind a curtain in the dark room.

"He shot at me twice and one bullet struck me on the leg," said Karie as he was taken to an awaiting ambulance.

26 wounded

The bodies of five girls thought to have been killed by the militants were found in the restaurant, said police. Inside the building, the body of a Syrian man who worked as a chef at restaurant lay near the rubble of a blood-spattered and bullet-marked wall.

APTOPIX Somalia Restaurant Blast

A mother whose daughter was shot in the head by attackers during the siege grieves in Mogadishu. (Farah Abdi Warsameh/Associated Press)

The attack began Wednesday evening a car bomb exploded at the gate to the restaurant and then gunmen posing as military forces stormed into the establishment. The roofs were blown off the restaurant and nearby buildings from the powerful blasts.

Most of the victims were young men who had been entering the Pizza House when the vehicle exploded, Saida Hussein said.

The gunmen "were dressed in military uniforms. They forced those fleeing the site to go inside" the restaurant, witness Nur Yasin told AP.

4,200 killed last year

Wednesday night's blast largely destroyed the restaurant's facade and sparked a fire. While al-Shabaab claimed to have attacked the neighbouring Posh Treats restaurant, which is frequented by the city's elite and was damaged in the blast, security officials said the Pizza House was targeted instead.

Security forces rescued Asian, Ethiopian, Kenyan and other workers at Posh Treats as the attack continued, Hussein said.

The Somalia-based al-Shabaab often targets high-profile areas of Mogadishu, including hotels, military checkpoints and areas near the presidential palace. It has vowed to step up attacks after the recently elected government launched a new military offensive against it.

Al-Shabaab last year became the deadliest Islamic extremist group in Africa, with more than 4,200 people killed in 2016, according to the Washington-based Africa Center for Strategic Studies.

'A critical moment for Somalia'

The extremist group also faces a new military push from the United States after President Donald Trump approved expanded operations, including airstrikes, against al-Shabaab. On Sunday, the U.S. military in Africa said it carried out an airstrike in southern Somalia that killed eight Islamic extremists at a rebel command and logistics camp.

Somalia President Mohamed Abdullahi Mohamed confirmed that airstrike and said such attacks would disrupt the group's ability to conduct new attacks.

With a new federal government established, pressure is growing on Somalia's military to assume full responsibility for the country's security. The 22,000-strong African Union multinational force, AMISOM, which has been supporting the fragile central government, plans to start withdrawing in 2018 and leave by the end of 2020.

Also Wednesday, the UN Security Council unanimously adopted a resolution extending the UN political mission in the Horn of Africa nation, which is trying to rebuild after more than two decades as a failed state, until March 31, 2018. The resolution recognized that "this is a critical moment for Somalia."