NAIROBI, Kenya — The gunmen went door to door in the Kenyan costal town, demanding to know if the men inside were Muslim and if they spoke Somali. If the extremists did not like the answers, they opened fire, witnesses said on Monday.

Al-Shabab, a Somali al-Qaida-linked group, claimed responsibility for the hours-long assault on Mpeketoni in which 48 people were killed. The attack began Sunday night as residents watched World Cup matches on TV and lasted until early Monday, with little resistance from Kenya’s security forces.

After daybreak, Kenyan troops and residents stared at the bodies lying on dirt streets by still-smoldering buildings. Two hotels and many vehicles were set on fire.

The attack highlights the growing incidents of Islamic extremist violence in a country that was once viewed as the bastion of stability in East Africa, drawing tourists from around the world for safaris and beach holidays. The U.S. ambassador made Kenya’s entire coastal region off-limits for embassy employees after the attack.

The merciless life-or-death religious assessment recalled al-Shabab’s attack on an upscale mall in Kenya’s capital, Nairobi, last September in which at least 67 people were killed, some of them after not being able to answer questions about Islam.

The Interior Ministry said that at about 8 p.m. Sunday, two minivans entered the town. Militants disembarked and began shooting. At the Breeze View Hotel, the gunmen pulled the men aside and ordered the women to watch as they killed them, saying it was what Kenyan troops are doing to Somali men inside Somalia, a police commander said on condition of anonymity because he was not authorized to share such details of the attack.

The several dozen gunmen also went door to door.

“They came to our house at around 8 p.m. and asked us in Swahili whether we were Muslims. My husband told them we were Christians and they shot him in the head and chest,” said Anne Gathigi.

Another resident, John Waweru, said his two brothers were killed because the attackers did not like that the brothers did not speak Somali.

“My brothers who stay next door to me were killed as I watched. I was peeping from my window and I clearly heard them speak to my brothers in Somali and it seems since my brothers did not meet their expectations, they sprayed them with bullets and moved on,” said Waweru.

Interior Minister Joseph Ole Lenku said the attackers fled into the nearby wilds, known as the Boni Forest after a “fierce exchange of fire” with security forces. He said 20 vehicles had been set on fire.

At a news conference, Ole Lenku was put on the defensive about the government’s security record after a string of attacks. He also warned opposition politicians against inciting violence, saying it was possible the attack was linked to politics. The claim was immediately dismissed by security experts.

Al-Shabab later said it carried out the attack because of Kenya’s “brutal oppression of Muslims in Kenya,” including the killings of Muslim scholars in Mombasa. The group said that such attacks would continue “as you continue to invade our lands and oppress innocent Muslims.”

Tourists were then warned: “Kenya is now officially a war zone and as such any tourists visiting the country do so at their own peril. Foreigners with any regard for their safety and security should stay away from Kenya.”

Kenya’s top police commander, David Kimaiyo, said the death toll was 48. A police spokeswoman said authorities believe that several dozen attackers took part.

Mpeketoni is about 20 miles (30 kilometers) southwest of the tourist center of Lamu. Any tourism in Mpeketoni is mostly local, with few foreigners visiting the area. The town is 60 miles (100 kilometers) from the Somali border and 360 miles (600 kilometers) from Nairobi.

Kenya has experienced a wave of gunfire and bomb and grenade attacks in recent months. The U.S., U.K., France, Australia, and Canada have all recently upgraded their terror threat warnings for the country. U.S. Marines behind sandbag bunkers have for some days been stationed on the roof of the U.S. Embassy in Nairobi.

Harald Kampa, who heads an association of hoteliers and caterers on the coast, said he wasn’t aware of any immediate tourist cancellations because of the attack. But he said the attack could soon prompt more cancellations. Kenya’s deteriorating security situation and travel warnings from foreign embassies has already hit the country’s tourism sector.

The region saw a spate of kidnappings of foreign tourists in 2011 that Kenya said was part of its motivation for attacking al-Shabab in Somalia. Since those attacks and subsequent terror warnings, tourism has dropped off sharply around Lamu, a UNESCO World Heritage Site and the country’s oldest continually inhabited town.

Al-Shabab claimed responsbility for a double bombing in Kampala, Uganda during the 2010 World Cup final that killed more than 70 people.