Hostages who failed an instruction to recite verses from the Koran were tortured and killed while those who passed the test were allowed to eat in the Bangladesh restaurant siege where at least 28 lost their lives.
The 10-hour hostage crisis that gripped the diplomatic zone in the capital Dhaka ended on Saturday morning with the death toll including six of the attackers.
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 night'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 dozens of 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, including kitchen staff, managed to escape by running to the rooftop 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 on Saturday morning along with his family.
"The gunmen asked everyone inside to recite from the Koran," the Islamic holy book, 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".
Detectives were questioning his son and his family along with other survivors as part of the investigation on Saturday, as scattered details of the siege emerged. Authorities were also interrogating one of the attackers captured by commandos in a dramatic morning rescue.
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" in the attack, warning that citizens of such countries would not be safe "as long as their warplanes kill Muslims".
The statement was circulated on Friday by IS supporters on the Telegram messaging service and resembled previous statements by IS.
The Amaq news agency, affiliated with IS, also posted photos purportedly showing hostages' bodies, though the authenticity of the images could not be confirmed.
The government did not directly comment on the IS 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 destabilise the nation - which the opposition denies.
On Saturday, Amaq published photos of five smiling young men each holding what appear to be assault rifles and posing in front of a black IS flags whom the agency identified as the attackers, according to the Site Intelligence Service, which monitors jihadi online activity.
They were identified by noms de guerre indicating they were all Bangladeshis. Amaq said the fighters used "knives, cleavers, assault rifles and hand grenades".
Amaq said the attackers "verified" the hostages identities, sparing the Muslims and killing the foreigners.
The 20 hostages killed included nine Italians, seven Japanese, three Bangladeshis and one Indian, government sources said, as details of the bloodshed began trickling from other capitals worldwide. The White House confirmed Saturday that a US citizen was among the hostages killed, but did not release any further identification.
"All the hostages were killed last night. The terrorists used sharp weapons to kill them brutally," said Brigadier General Nayeem Ashfaq Chowdhury of the Army Headquarters in a news conference.
Two Bangladeshi police officers also died from injuries sustained while exchanging gunfire with the attackers.
A Roman Catholic priest in southern Italy, whose 33-year-old sister Simona Monti, a textile firm employee, was killed in the attack expressed hope that her death could contribute toward making a more just world. Father Luca Monti said he hopes "this experience of martyrdom for my family and the blood of my sister Simona can help contribute to building a more just and brotherly world".
In New Delhi, Foreign Minister Sushma Swaraj said she was "extremely pained to share that the terrorists have killed Tarushi, an Indian girl who was taken hostage in the terror attack in Dhaka."
Emory University in Oxford, Georgia, said two of its students were killed in the attack: Abinta Kabir, of Miami, Florida, who was visiting family and friends in Bangladesh, and Faraaz Hossain, of Dhaka, who had completed his second year on the Oxford campus.
Ten of 26 people who were wounded when the militants opened fire were in critical condition, and six were on life support, according to hospital staff. The injuries ranged from broken bones to gunshot wounds. Most of them were police officers, but one was a civilian.
In the end, paramilitary troops managed to rescue 13 hostages, including one Argentinian, two Sri Lankans and two Bangladeshis, according to Lieutenant Colonel Tuhin Mohammad Masud, commander of the Rapid Action Battalion that conducted the rescue operations. Japan's government said one Japanese hostage was also rescued with a gunshot wound.
"Because of the effort of the joint force, the terrorists could not flee," the prime minister Sheikh Hasina said in a nationally televised speech, vowing to fight militant attacks in the country and urged people to come forward.
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 with families.
"Anyone who believes in religion cannot do such an act," the prime minister, said. "They do not have any religion, their only religion is terrorism."
She announced two days of national mourning for the dead.
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. IS 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. That the attackers targeted a popular restaurant in the heart of the diplomatic quarter of Bangladesh's capital signalled a change in tactics. The restaurant overlooking a lake serves Spanish food and is patronised by residents of Gulshan, an affluent neighbourhood where most of the foreign embassies are located.