DHAKA, Bangladesh — The cook was crouching in a washroom, taking refuge from the gunmen who had invaded the Holey Artisan Bakery, when he understood that there was a logic behind the killing: The people in the restaurant were being sorted.
“Bengali people, come out,” one gunman shouted. When the cook, Sumir Barai, and eight other men opened the bathroom door, trembling, they saw two young men, clean shaven and dressed in jeans and T-shirts.
“You don’t need to be so tense,” one of the men told them. “We will not kill Bengalis. We will only kill foreigners.” At that, Mr. Barai’s gaze flicked to the floor of the restaurant, where he could see six or seven bodies, apparently shot and then sliced with machetes. All appeared to be foreigners.
The gunmen, he said, seemed eager to see their actions amplified on social media: After killing the patrons, they asked the staff to turn on the restaurant’s wireless network. Then they used customers’ telephones to post images of the bodies on the internet.
Friday night’s assault on the Holey Artisan Bakery in the diplomatic district of Dhaka, in which at least 20 hostages and two police officers were killed, marks a scaling up of ambition and capacity for Bangladesh’s Islamist militancy, which has until now carried out pinpoint assassinations, mostly of critics of Islam and members of religious minorities.
Among the dead from Friday’s attack, the police said, were nine Italians, seven Japanese, two Bangladeshis, one American and one Indian.
The attack also suggests that Bangladesh’s militant networks are internationalizing, a key concern as the United States seeks to contain the growth of the Islamic State.
Bangladesh’s 160 million people are almost all Sunni Muslims, including a demographic bulge under the age of 25. This makes it valuable as a recruiting ground for the Islamic State, now under pressure in its core territory of Iraq and Syria. Western intelligence officials have been watching the organization pivot to missions elsewhere in the world, launching attacks on far-flung civilian targets that are difficult to deter with traditional military campaigns.
“We need to take serious stock of the overall threat,” said Shafqat Munir, a research fellow at the Bangladesh Institute of Peace and Security Studies. “There were all sorts of warnings and signs and everything. But I don’t think anyone expected anything as audacious and large-scale as this.”
It was a slow night at the restaurant. Eighteen people had reserved seats at the Holey Artisan Bakery, whose crusty flour-dusted loaves of bread and piles of homemade pasta offered a respite from the sticky, clamorous city that surrounded it.
Seven Italian friends had gathered around one table, and three or four at a second, recalled Diego Rossini, a chef who is from Argentina. Someone had just ordered an Italian pasta dish, and Mr. Rossini made his way to the kitchen, preparing for a much larger crowd that was expected at 9:30 p.m.
But at 8:45, a half-dozen young men entered, carrying heavy bags of weaponry, including grenades and long rifles. Mr. Rossini, the chef, fled to the roof. He heard screams, and shouts of “Allahu akbar,” as the gunmen sought out patrons who were hiding.
“There were a lot of foreigners,” he told Canal 5 Noticias, an Argentine cable news station. “That’s who they were particularly looking for.”
Even as they killed the foreigners, the attackers were unfailingly polite and solicitous with the restaurant staff and other Bangladeshis, Mr. Barai said.
They took the staff into their confidence, complaining that foreigners, with their skimpy clothes and taste for alcohol, were impeding the spread of Islam. “Their lifestyle is encouraging local people to do the same thing,” a militant said.
They asked the staff to make coffee and tea and serve it to the remaining hostages. At 3:30 a.m., when Muslims eat a predawn meal before fasting, they asked the kitchen staff to prepare and serve dishes of fish and shrimp, he said.
Mr. Barai recalls being puzzled by the attackers, who spoke cosmopolitan Bengali, and even some English, when conversing with the foreigners.
“They were all smart and handsome and educated,” he said. “If you look at those guys, nobody could believe they could do this.” In the predawn hours, the militants lectured their captives on religious practices, instructing the kitchen staff to say regular prayers and study the Quran.
Early in the morning, the gunmen released a group of women wearing hijabs and offered a young Bangladeshi man, Faraz Hossain, the opportunity to leave, too, said Hishaam Hossain, Mr. Hossain’s nephew, who had heard an account from the hostages who were freed.
Mr. Hossain, a student at Emory University, was accompanied by two women wearing Western clothes, however, and when the gunmen asked the women where they were from, they said India and the United States. The gunmen refused to release them, and Mr. Hossain refused to leave them behind, his relative said. He would be among those found dead on Saturday morning.
In the hours after the gunmen appeared, hundreds of police officers massed outside the restaurant compound’s walls, but an attempted raid was repulsed by a grenade, killing two officers and injuring more than 20. Mr. Rossini, who was on the roof, frantically texted his location over social media.
“It was practically impossible for the police to get in,” he said later. The restaurant was like a little fort, and the police had to wait for the army.
A senior police official, speaking to a reporter on the condition of anonymity, said that the police tried unsuccessfully to establish contact with the captors, who never passed on any demands.
The night crept on with painful slowness in the crowded washroom, where Mr. Barai and the eight other men were again locked in, this time by the gunmen. At 1:44 a.m. Mr. Barai messaged a cousin, who was only a few yards away, outside the police cordon.
“What is the news on the outside?” His cousin typed back that a Rapid Action Battalion, Bangladesh’s elite counterterrorism squad, was now involved in the operation. “They are not doing anything right now so you people don’t become victims,” he wrote.
Mr. Barai passed on the name of a co-worker who could lead authorities to the washroom. “We are here,” he typed. “If possible break the wall of the toilet and rescue us.”
As dawn approached, Mr. Barai feared that the men would suffocate in the cubicle, which measures about four feet by four feet. “Please come to the toilet quickly as it is very difficult inside the toilet.”
After that, when Mr. Barai’s cousin called his number, there was no answer; the cousin, seated on the sidewalk outside the restaurant, began to sob.
In an early-morning meeting at her residence, Prime Minister Sheikh Hasina had authorized a military raid, but that required transporting a team of commandos via C-130 from Sylhet, roughly 150 miles from the capital.
Shortly after sunrise, dozens of armed personnel carriers formed columns in the lanes around the restaurant.
Mr. Barai said the surviving hostages sensed that the siege was ending. During the long hours that passed inside the restaurant, the gunmen made it clear that they expected to die, Mr. Barai said. One of them calmly said as much. “You see what we did here,” the militant said, pointing to the bodies around. “The same thing is going to happen to us now.”
At 7:30 a.m., he said, the militants told them: “We are leaving. See you in heaven.”
They were getting ready to walk out the door, he said, when the commandos stormed the restaurant.
An earlier version of this article misstated the name of a cook at the restaurant who hid in a washroom with eight other men. He is Sumir Barai, not Soumir Roy.
Because of an editing error, an earlier version of the reporting credit with this article misstated the location of one of the reporters. Geeta Anand reported from Mumbai, not New Delhi.