A man collects the belongings of victims from a Rawda mosque on Nov. 25, a day after the mosque was attacked in the Egyptian village. (EPA-EFE/Rex/Shutterstock)

Before their village became a killing field, it was a sanctuary.

Many of the 305 people slaughtered at a mosque in the village of Rawda on Friday had moved there to escape clashes between the Islamic State and Egyptian security forces elsewhere in Egypt’s Sinai Peninsula, witnesses said.

The village had become sort of a motherland to its mostly Sufi Muslim residents, who saw safety in numbers.

But in recent months, the militants had chased down the displaced to Rawda. They repeatedly ordered the Sufis to give up their rituals — or face death. The community reported the threats to the military, villagers said, but no added protection arrived.

At least 305 people were killed in Egypt’s Sinai Peninsula after militants detonated a bomb and shot at worshipers in a mosque on Nov. 24. The attack is the deadliest assault on Egyptian civilians by militants in recent years. (Joyce Lee/The Washington Post)

So the residents erected four-foot-high sand barriers around the mosque and nearby roads in a futile effort to protect themselves.

“After the threats, we were expecting an attack, but not something so savage,” said Yussef Mustafa, 37, a government employee who lost three brothers in the assault.

The massacre in Rawda highlights the vulnerability of communities trapped in a conflict between Egyptian security forces and one of the Islamic State’s most virulent affiliates. The attack, the deadliest in Egypt’s modern history, is the latest sign of the government’s inability to contain a spreading insurgency that is becoming more brazen and ambitious.

No group has asserted responsibility for Friday’s bloodshed. But the Islamic State affiliate known as Wilayat Sinai is active in the area, and witnesses said some of the attackers carried a black Islamic State flag.

Wilayat Sinai has long relied on the sympathies of disaffected Sinai tribes ignored by the government and drawn its leaders and fighters from local communities. But particularly striking in Friday’s attack was that most of the victims belonged to a clan of one of the most powerful tribes in the Sinai.

That suggests an important shift for the group: Even though most of the leadership still hails from the Sinai, its membership now includes many more outsiders with no ties to the region, analysts and residents said.

Membership has been bolstered by returnees who fought with the Islamic State, also known as ISIS, in Syria and Iraq, according to Zack Gold, a nonresident fellow at the Washington-based Atlantic Council.

“These outside fighters — even if Egyptians, they’re ‘foreign’ to North Sinai — don’t care about tribal interests and red lines,” he said.

Friday’s carnage was part of a broader shift toward sectarianism for the Islamic State branch, both in the Sinai and on the Egyptian mainland. While sectarian attacks have been a hallmark of militants’ tactics in Syria and Iraq, they have gained prominence in Egypt only within the past year.

In the past, the Islamic State attacked mostly soldiers and police. But more recently, the militants have targeted the country’s Coptic Christian minority with devastating bombings of churches in Cairo, Alexandria and other cities, as well as assassinations, forcing thousands of Christians to flee the Sinai.

“The Rawda mosque attack and other high-casualty incidents afford the Islamic State the media attention that it seeks, delegitimize the efforts of the Egyptian government and further the group’s global sectarian ambitions,” said Jacob Greene, a research associate at the Tahrir Institute for Middle East Policy.

The Sufis of Rawda understood their precarious existence.

They believe in a moderate brand of Islam that promotes tolerance and pluralism. But the Islamic State and other Sunni extremists have long considered Sufism as heretical. In Mali, Pakistan and elsewhere, the Islamic State has targeted Sufi mosques and shrines.

In November 2016, the Islamic State kidnapped and beheaded Sulaiman Abu Haraz, a prominent Sufi cleric believed to have been 100 years old. The Islamic State accused him of practicing witchcraft. In a photo released by the militants, a masked man in black attire is seen wielding a large, curved sword behind the head of the cleric, who was associated with the Rawda mosque. One of Haraz’s students was also executed.

In an Islamic State magazine interview two months later, a senior militant commander in the Sinai identified Rawda, and the district where it’s located, as one of three Sufi areas the group hoped to “eradicate.” The commander said that Sufis deserve to be killed “if they don’t repent” and accused them of “collaborating with the oppressor.”

Egyptian military personnel passed through the area and at times conducted operations but never stayed. And they never came during Friday prayers, the main religious gathering of the week that always attracted hundreds of villagers, residents said.

Meanwhile, Islamic State fighters were moving closer to the village, which is on the northern tip of the Sinai Peninsula, west of the provincial capital Arish.

The conflict is a deadly game of cat and mouse — where Egyptian forces are fighting with tanks, armored vehicles and other conventional weapons, and Islamic State fighters stage hit-and-run attacks, mostly on army checkpoints and outposts.

Military operations had pushed the group out of the central Sinai and toward Arish. But in recent months, the fighters have moved west toward the mountains near Rawda, according to residents and local journalists.

Two weeks ago, an Islamic State member distributed leaflets in the village ordering Sufi scholars to stop “heretical actions,” according to two local journalists. “So the people closed the main road leading to the mosque,” said Mohamed Elhor, one of the journalists.

But villagers continued their daily lives.

Khalid Soleiman, 22, and his large extended Sufi family arrived nearly two years ago after clashes broke out between militants and the military in the Bedouin town of Sheikh Zuweid, southeast of Arish. “It was unsafe to live there,” said Soleiman, who is studying to be a pharmacist.

Last Friday, he was on his way to the mosque for the traditional Juma prayer. Hundreds of men and boys had gathered inside and outside the building. Women were not present, as is typical in many conservative communities.

As Soleiman approached the mosque, more than two dozen militants arrived in several pickups. Some fanned out and gunned down panicked worshipers, while others entered the mosque to kill more. The microphone inside was on during the carnage.

“We could hear people scream,” recalled Soleiman, who hid behind a house. “I could hear the militants yell: ‘Kill them all. Young and old. Don’t spare anyone.’ ”

Soleiman tried to escape from the house. But as he ran, a bullet grazed his leg. He took cover inside a chicken coop of another house.

Some of the gunmen executed the wounded and anyone who tried to surrender. Others set cars on fire.

Most of the fighters, he said, had Egyptian accents. Some had reddish beards, he said, suggesting to him that they were foreigners. Roughly a half-hour later, the gunmen jumped into their vehicles and sped into the mountainsThe first body Soleiman found at the mosque was his uncle.

He would find the bodies of 17 more relatives.

By some estimates, at least a quarter of the male population of Rawda is dead.

“There is no future for the village,” Soleiman said. “Unless we wait for the children to grow up and start their own families.”

Louisa Loveluck contributed to this report.