As the Islamic State (IS) group claimed responsibility for the carnage at the start of the Eid holiday, Prime Minister Sheikh Hasina said she was determined to eradicate militancy in the mainly Muslim nation.
Survivors told of how the hostage-takers separated locals from the foreigners who were eating side-by-side before embarking on a killing spree which was brought to an end 11 hours later in a fierce gunbattle.
Although there was no exact breakdown of the casualties, the army said most of the slain civilians were either Italian or Japanese. Most had been slaughtered with sharpened weapons.
Two police officers were also killed at the start of the siege while a total of six attackers were shot dead at its finale.
One of the assailants was captured alive and arrested, the army said.
Hasina's government has previously blamed a string of deadly attacks, targeting religious minorities and foreigners, on her domestic opponents but the incident will heighten fears that IS's reach is spreading.
"It was an extremely heinous act. What kind of Muslims are these people?" said Hasina in a televised address.
The attack, by far the deadliest of a recent wave of killings claimed by IS or a local Al-Qaeda offshoot, was carried out in a neighbourhood which is home to the country's elite and houses many of the major embassies.
Sharp weapons
Announcing the end of the siege, officials said that 13 hostages had been rescued after members of an elite force took control of the cafe and shot dead six of the gunmen.
But while Hasina called the outcome a "success", the security forces later revealed that 20 of those taken captive were killed.
"We've recovered 20 bodies. Most them had been brutally hacked to death with sharp weapons," an army spokesman, Brigadier General Nayeem Ashfaq Chowdhury, told reporters.
Another senior army officer then confirmed that all 20 were foreigners.
"All 20 of the hostages who were killed were foreigners.... Most of them were Italian or Japanese," Lieutenant Shahab Uddin told AFP without giving an exact breakdown.
A Japanese government official said its Prime Minister Shinzo Abe had told Hasina in a phone call that several Japanese nationals may have been inside. Italian Foreign Minister Paolo Gentiloni also talked on the phone with his Bangladeshi counterpart Abul Hassan Mahmood Ali.
Foreigners and Bangladeshis could be seen standing outside the upmarket Holey Artisan Bakery cafe after the siege, still awaiting news on their loved ones inside. Commandos could also be seen roaming nearby rooftops.
Witnesses recounted how a massive gunfight erupted on Saturday morning as more than 100 commandos launched the rescue operation, nearly 11 hours after the siege began shortly before 9.30 pm.
Eight hostages including a foreigner were rescued in the first few minutes of the operation. TV footage showed ambulances rushing some of those who had been freed to a military hospital.
Dodging bullets
"It was a horrendous night," said Diego Rossini, an Argentine chef who managed to escape through a terrace during the siege.
"They (the hostage-takers) had automatic weapons and bombs," he said on Argentinian TV as he described how he eventually managed to escape into the next-door building despite coming under fire.
"I felt bullets pass so close to me, I felt fear like I've never felt in my life."
The father of one of the survivors was told by his son how the hostage-takers separated the locals from foreigners.
"They (the foreigners) were taken to the upper floor and the Bangladeshis were kept around a table," Rezaul Karim told AFP.
"My daughter-in-law wears a hijab. Maybe that helped save the whole family."
Fierce firefight
Heavily armed police and paramilitary guards cordoned off the area around the restaurant after the militants launched their attack and then became involved in a fierce firefight with police.
Police said that two officers, including the head of the local police station were killed. Hasina said that 30 officers were injured.
The attack follows a series of killings targeting religious minorities and foreigners.
Earlier Friday, a Hindu temple worker was hacked to death in western Bangladesh and a Hindu priest was stabbed and critically wounded early Saturday in the southwest of the country.
The government and police blame homegrown militants for the killings, which they say are part of a plot to destabilise the country.
Bangladesh's main Islamist party has been banned from contesting polls and most of its leaders have been arrested or else executed after recent trials over their role in the 1971 war of independence from Pakistan.
Hasina called on all Bangladeshis to "resist these terrorists".
"My government is determined to root out terrorism and militancy from Bangladesh," she added.
Last month authorities launched a nationwide crackdown on local jihadist groups, arresting more than 11,000 people, under pressure to act on the spate of killings.
But many rights groups allege the arrests were arbitrary or were a way to silence political opponents of the government.