Police said at least 13 hostages had been freed from the Holey Artisan Bakery in Dhaka, where one suspected gunman was captured in a military operation.
An Isis-affiliated propaganda agency released photos from inside the restaurant during the raid, appearing to showing the bodies of women and men on the floor in pools of blood.
The group released a statement saying “Islamic State commandos” attacked the restaurant, describing it as “frequented by foreigners”.
Bangladeshi police would not confirm the terrorists’ claim to have killed 24 victims and wounded 40, saying that two police officers had died, but a military official later said 20 mostly foreign hostages were also dead.
The father of one of the survivors said his son was held hostage for 10 hours.
Rezaul Karim told Bangladesh's Daily Star newspaper: “The gunmen were doing a background check on religion by asking everyone to recite from the Quran.
“Those who could recite a verse or two were spared. The others were tortured.”
Army Brigadier General Nayeem Ashfaq Chowdhury said most of the hostages were "killed mercilessly by sharp weapons" but local reports of beheadings were unconfirmed.
The bodies of nine Italians were identified after the attack, foreign minister Paolo Gentiloni said.
Seven Japanese people, five men and two women who worked as consultants for the country’s international development agency, were also killed.
An Indian teenager was among the hostages murdered, foreign minister Sushma Swaraj said.
She named the victim as Tarushi Jain, 19, and said she had attended the city's American School and studied at Berkeley university in the US.
Two other victims were from at Emory University in Georgia. Abinta Kabir was a student from Miami who had been visiting her family and friends in Bangladesh.
University president James Wagner said that Abinta's mother was in “unspeakable pain” upon receiving news of the death of her daughter.
Faraaz Hossain, from Dhaka, graduated from the university’s Oxford College this year and was moving on to its Goizueta Business School.
“The Emory community mourns this tragic and senseless loss of two members of our university family,” a spokesperson said.
“Our thoughts and prayers go out on behalf of Faraaz and Abinta and their families and friends for strength and peace at this unspeakably sad time.”
The White House said an American citizen was killed but did not confirm their identity.
Philip Hammond said the Foreign and Commonwealth Office was investigating whether any British citizens were caught up in the "appalling act of violence" and sent his thoughts to the victims and their families.
Witnesses described up to nine militants shouting “Allahu Akbar”, meaning “God is great” as they burst into the Holey Artisan Bakery at 9.20pm local time on Friday (4.20pm BST).
A police officer at the scene said that when security forces tried to enter the premises at the beginning of the siege they were met with a hail of bullets and grenades.
Assailants exchanged sporadic gunfire with police outside for several hours as negotiators attempted to contact the militants before troops raided the building.
Lori Ann Walsh Imdad, principal of the nearby American Standard School, told The Independent: “I’ve been hearing gunshots all night long and I’ve seen people running.”
Gowher Rizvi, an adviser to Bangladeshi Prime Minister Sheikh Hasina, said the crisis started when security guards in the Gulshan district of Dhaka noticed several gunmen outside a medical centre.
When the guards approached, the gunmen ran into the Holey Artisan cafe, which was packed with people waiting for tables, he said.
A cafe employee who escaped told local television that around 20 customers were in the restaurant at the time, most of them foreigners, and 15 to 20 staff.
The Holey Artisan Bakery, popular with foreign officials and expatriates, sits in the affluent diplomatic quarter of Bangladesh’s capital near the embassies of the US, Turkey, Germany and other nations.
Bangladeshi’s Prime Minister, Sheikh Hasina, vowed to fight terrorism and urged people with information to come forward.
“Because of the effort of the joint force, the terrorists could not flee,” she said.
“Anyone who believes in religion cannot do such act. They do not have any religion, their only religion is terrorism.”
The restaurant attack marked a major escalation in two years of increasingly frequent atrocities by Islamist militants in Bangladesh, sparking hundreds of arrests.
A string of machete attacks have been claimed by Isis, al-Qaeda and local groups, targeting secular writers, LGBT activists and religious minorities among others, with the latest victim being a Hindu priest hacked to death at a temple in Jhenaidah on Friday.
Bangladeshi authorities have persistently denied Isis or al-Qaeda have a presence in the country, sparking warnings from counter-terror monitors including the Site Intelligence Group, which urged the government to “face the truth.”
Security officials say two local militant groups, Ansar-al-Islam and Jamaat-ul-Mujahideen, are responsible for the violence.
Isis announced its presence in the country late last year and analysts believe they are recruiting from pre-existing extremist groups who have been carrying out attacks since 2013.
It has been increasingly advertising its alleged expansion into the country through official propaganda channels, with two large features on the subject in a recent issue of its English language magazine.
Calling the faction its “Bengal” province, Isis has celebrated several machete attacks and vowed to continue targeting Shia and Ahmadi Muslims, “crusaders”, Hindus and missionaries.
Join our new commenting forum
Join thought-provoking conversations, follow other Independent readers and see their replies