NAIROBI — A series of car bombs killed at least 20 people Friday in Somalia’s capital, Mogadishu, in attacks claimed by the al-Shabab extremist group.

At least three bombs exploded outside a hotel near the headquarters of Somalia’s Criminal Investigations Department, and a gun battle ensued when four militants tried unsuccessfully to storm the hotel, officials said. The Associated Press reported that a fourth bomb went off as medics attempted to rescue injured people.

Somali officials said police officers and hotel security guards shot and killed four militants as they attempted to burst through a hole that one of the blasts opened in a wall of the Sahafi Hotel, which is frequented by government officials and appeared to be the main target of the attacks. At least two other militants were reported killed when they detonated their suicide car bombs.

According to witnesses, the first car bomb hit the Sahafi Hotel, adjacent to the Hayat Hotel on a busy street in Mogadishu. The second car detonated on a street that lies between the Criminal Investigations Department and the Sahafi Hotel, while a third bomb exploded on another street behind the hotel.

The bombs went off just minutes after 4 p.m. when the streets were packed with pedestrians and traffic. Cars, minibuses and motorcycles were incinerated by the blasts. At least 17 people were reported injured in the explosions.

A spokesman for al-Shabab, an Islamist group linked to the al-Qaeda terrorist network, said the hotel was targeted because “it acts as a government base,” the Reuters news agency reported. “Government officials and security forces are always in the hotel,” said the spokesman, Abdiasisi Abu Musab.

Among those killed was Abdifatah Abdirashid, the manager of the Sahafi Hotel, news agencies reported. He took over the hotel after his father was killed in an al-Shabab attack on the building in 2015.

Al-Shabab continues to control large parts of southern and central Somalia. It has carried out deadly attacks in the past on high-profile targets in Mogadishu and other cities and towns, including places in neighboring Kenya.

Last month, Somalia marked the anniversary of one of al-Shabab’s deadliest attacks, a 2017 truck bombing in Mogadishu in that killed more than 500 people.

Supporting Somalia’s federal government in its fight against al-Shabab is a regional peacekeeping force known as the African Union Mission in Somalia (AMISOM).

The U.N.-backed mission, with contributing troops from Uganda, Ethiopia, Djibouti, Kenya and Burundi, has been helping the Somali government strengthen its national army as part of a plan to reclaim parts of the country controlled by al-Shabab.

Last year, the mission announced that it would reduce its troops in Somalia, a process that would continue through 2021. However, contributing states remain concerned about insecurity in the region and worry that Somalia’s forces are not yet ready to assume full responsibility for national security.