A suicide bomber detonated explosives outside a hotel in a popular beachfront area in Somalia’s capital, Mogadishu, before gunmen stormed the building, setting off a four-hour siege that left at least 32 people dead and more than 60 others wounded, the police said on Saturday.

The Islamist militant group Al Shabab claimed responsibility for the deadly assault, which started late Friday. Al Shabab have been waging an insurgency against the internationally backed government in Somalia for more than 17 years and have previously targeted the beach area, Lido, which is popular with businesspeople and officials as well as with other residents.

A witness, Mohamed Jibril, said that, at the time of the attack, he had been out with friends in the area, which had been thronged with hundreds of people.

“We heard a loud explosion followed by gunfire,” Mr. Jibril said in an interview.

“I have never seen anything like that in my entire life,” he added. “I saw many people lying on the beach asking for help, and no one dared to help them because there was ongoing shooting.”

A spokesman for the Somali police, Abdifatah Adan Hassan, said that officers had killed three attackers who had stormed the beachfront hotel, ending the siege.

“Our security forces have eliminated all three Shabab attackers who entered the hotel and took hostage the customers and beachgoers who took shelter inside the building,” he said.

Al Shabab have claimed numerous bombings and attacks in Mogadishu and other parts of Somalia as the government presses an offensive against the militants.

The Lido area has been the target of several previous attacks, including a six-hour siege by Al Shabab on a beachside hotel in 2023, which left six civilians dead and 10 wounded. Five people were killed in a car bomb blast at a cafe in the capital last month. And in March, the militants killed three people and wounded 27 in an hourslong siege of another Mogadishu hotel, breaking a relative lull in the fighting.