DHAKA, Bangladesh —The hostages were given a test: Recite verses from the Quran, or be punished, according to a witness. Those who passed were allowed to eat. Those who failed were tortured and slain.

The dramatic, 10-hour hostage crisis that gripped Bangladesh’s diplomatic zone ended Saturday morning with at least 28 dead, including six of the attackers, as commandos raided the popular restaurant where heavily armed attackers were holding dozens of foreigners and Bangladeshis prisoner while hurling bombs and engaging in a gunbattle with security forces. The victims included 20 hostages, mostly foreigners, and two Bangladeshi police officers.

The attack marks an escalation in militant violence that has hit the traditionally moderate Muslim-majority nation with increasing frequency in recent months, with the extremists demanding the secular government revert to Islamic rule. Most previous attacks have involved machete-wielding men singling out individual activists, foreigners and religious minorities.

But Friday’s attack was different, more coordinated, with the attackers brandishing assault rifles as they shouted “Allahu Akbar” (God is Great) and stormed the Holey Artisan Bakery in Dhaka’s Gulshan area while foreigners and Bangladeshis were dining out during the Ramadan holy month.

The gunmen, initially firing blanks, ordered restaurant workers to switch off the lights, and they draped black cloths over closed-circuit cameras, according to a survivor, who spoke with local TV channel ATN News. He and others managed to escape by running to the roof or out the back door.

But about 35 were trapped inside, their fate depending on whether they could prove themselves to be Muslims, according to the father of a Bangladeshi businessman who was rescued Saturday morning along with his family.

“The gunmen asked everyone inside to recite from the Quran,” according to Rezaul Karim, describing what his son, Hasnat, had witnessed inside. “Those who recited were spared. The gunmen even gave them meals last night.”

The others, he said, “were tortured.”

It was not immediately clear whether the attackers had a specific goal, and Bangladesh authorities would not say if they had made any demands.

The Islamic State group claimed responsibility, saying it targeted the citizens of “Crusader countries,” warning that citizens of such countries would not be safe “as long as their warplanes kill Muslims.”.

The government did not directly comment on the Islamic State’s claim but has denied in the past that the extremist group has a presence in Bangladesh. The government of Prime Minister Sheikh Hasina instead has accused her political enemies of orchestrating the violence in order to destabilize the nation – which the opposition denies.

The audacious attack came during Ramadan, when devout Muslims fast during the day and eat after dark. Many left the city of more than 10 million people for a nine-day public holiday with families to celebrate Eid al Fitr festival.

“Anyone who believes in religion cannot do such an act,” Hasina, the prime minister, said. “They do not have any religion, their only religion is terrorism.”

The government has cracked down on domestic radical Islamists by making scores of arrests. It has blamed local terrorists and opposition political parties – especially the main opposition Bangladesh Nationalist Party and its Islamist ally Jamaat-e-Islami.

But the attacks have continued, with about two dozen atheist writers, publishers, members of religious minorities, social activists and foreign aid workers slain since 2013. Earlier on Friday, a Hindu temple worker was hacked to death by at least three assailants in southwest Bangladesh. Islamic State and and al-Qaida affiliates have claimed responsibility for many of those attacks.

The escalating violence leading up to the unprecedented hostage crisis has raised fears that religious extremists are gaining a foothold in the country, despite its traditions of secularism and tolerance.