Islamist militants from Somalia have said they carried out an attack on the Kenyan coast that left at least 48 people dead, and warned foreign tourists that the east African country was now "officially a war zone".

Gunmen reportedly went door to door in the coastal town of Mpeketoni on Sunday evening, demanding to know if the men inside were Somali-speaking Muslims. Those who were not were shot dead. Among the victims were football fans who had been watching a World Cup match at a video hall. Others were dragged from their homes or some of the town's small hotels.

The brutal separation of Muslims from non-Muslims echoed the assault on the Westgate shopping mall in Nairobi last September when at least 67 people were killed, some after not being able to answer questions about Islam. The Somali militant group al-Shabaab has previously vowed to carry out terror attacks to avenge Kenyan troops' presence in Somalia.

A statement from al-Shabaab on Monday said the latest attack – the deadliest since Westgate – was revenge for the "Kenyan government's brutal oppression of Muslims in Kenya through coercion, intimidation and extrajudicial killings of Muslim scholars".

It also condemned the "Kenyan military's continued invasion and occupation of our Muslim lands and the massacre of innocent Muslims in Somalia. To the tourists visiting Kenya we say this: Kenya is now officially a war zone and as such any tourists visiting the country do so at their own peril."

The statement added: "Foreigners with any regard for their safety and security should stay away from Kenya or suffer the bitter consequences of their folly. You have been forewarned!"

Mpeketoni, a trading centre on the main coastal road, lies on the mainland 20 miles south-west of Lamu island, a popular tourist destination whose ancient architecture is listed as a Unesco world heritage site.

As dawn rose in Mpeketoni on Monday, the Guardian saw at least 10 bodies lying in the streets or next to houses, almost all adult men. Most had been shot, but some had been hacked with knives or other sharp objects.

By late morning, dozens more bloodied corpses had been brought to the town's tiny mortuary, where they lay three to four deep, some burned beyond recognition. Several people were killed in and around the nearby village of Kibaoni.

Witnesses said the attackers – numbering more than 30, many wearing cloths to cover their faces – had stormed into town at about 8.30pm on Sunday in two hijacked minibuses, meeting little resistance from security forces.

They divided into groups, one of which attacked a police station while another attacked banks and business premises. At least two hotels and many vehicles were set on fire during a methodical assault that lasted all night.

John Waweru, who lost two brothers in the attack, said the gunmen spoke Somali and asked his brothers to recite verses from the Qur'an. "I heard them speaking some Somali to my brothers who live next door," he recalled. "Then I heard a burst of gunfire as they sprayed the house with bullets. I hid myself and went to check on the house after they left and found them shot in the head."

Another survivor, Anne Gathigi, said the gunmen also spoke Swahili and asked her husband his religion. "When he said he was a Christian, they just opened fire. They shot him five times in the head and chest."

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 were doing to Somali men inside Somalia, a police commander told the Associated Press.

Some of the victims had been watching the World Cup in local bars and hotels. Meshack Kimani told Reuters: "The attackers were so many and were all armed with guns. They entered the video hall where we were watching a World Cup match and shot indiscriminately at us. They targeted only men but I was lucky. I escaped by hiding behind the door."

Samuel Gitobu, a motorbike rider, recalled: "I had just dropped clients at Equity bank when we saw smoke and later heard gunshots. At first I thought it was a robbery but other motorbike riders fled, shouting, 'al-Shabaab!'"

Gitobu said he called a local police officer about the incident but his phone went unanswered. A junior police officer later called him back, telling him that they had also been ambushed.

"One of the police officers told me they were under attack too and could not establish the number of the attackers," he added. "He said the officers would respond after they repulsed the gunmen. But they only came at around 7am to collect bodies lying around the town and surrounding villages."

Diana Salim was driving passengers in a minivan north from his hometown of Malindi, south of Mpeketoni, when more than 20 heavily armed men stopped his and another vehicle. Speaking from his hospital bed on Lamu, he described how he and the other passengers were ordered into the bush.

"They told us to lie down and started beating us," he said. "When they put a pistol to my head, I shouted out the shahada [the profession of faith in Islam] and they said that I should run away because I was a Muslim."

After walking for miles, Salim found his way back to the road and managed to hitch a ride to Lamu. He believes that the vehicle he was driving may have been used in the subsequent attack, but did not know what happened to the passengers who remained in the bush.

Kenya's 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. A search is under way.

After Sunday's attack, the American ambassador made Kenya's entire coastal region off limits for embassy employees.

Kenyan troops crossed into southern Somalia in 2011 to fight al-Shabaab, later joining the now 22,000-strong African Union force. Since then a series of reprisal terrorist attacks has damaged Kenya's crucial tourist industry. Tour operators evacuated hundreds of British holidaymakers last month from beach resorts near the port city of Mombasa following new warnings from the Foreign Office.

The statement from al-Shabaab added: "We hereby warn the Kenyan government and its public that as long as you continue to invade our lands and oppress innocent Muslims, such attacks will continue and the prospect of peace and stability in Kenya will be but a distant mirage. Do not ever dream of living peacefully in your lands while your forces kill the innocent in our lands."