Islamic State said it was responsible for one of the most brazen attacks in the South Asian nation's history, but that claim has yet to be confirmed.
It marks a major escalation in a campaign by militants over the past 18 months that had targeted mostly individuals advocating a secular or liberal lifestyle in majority-Muslim Bangladesh with 160 million people.
The gunmen, who stormed the busy restaurant in Dhaka's diplomatic area late on Friday night, ordered all Bangladeshis to stand up before they began killing foreigners, a source briefed on the police investigation said.
"Let the people of the crusader countries know that there is no safety for them as long as their aircraft are killing Muslims," Islamic State said in a statement, also posting pictures of five fighters it said were involved in the attack.
Among the dead was the wife of an Italian businessman killed by a machete. She was found by her husband after he spent all night hiding behind a tree outside the cafe while the gunmen were inside, said Agnese Barolo, a friend who lives in Dhaka and spoke to him.
Nine Italians were killed in the attack, the country's foreign minister said, and authorities were trying to confirm the fate of another person missing.
Japan's Chief Cabinet Secretary Yoshihide Suga said seven of its citizens had been confirmed dead in the attack, while one Indian, a 19 year-old female student, was killed in the assault, India's foreign minister said on Twitter.
Emory University in Atlanta said in a statement that two of its students were among hostages who were killed. Abinta Kabir, from Miami, was an undergraduate at Emory's Oxford College, and Faraaz Hossain, who was from Dhaka, was a graduate student at the university's Goizueta Business School, Emory said.
The U.S. State Department confirmed one of those killed was a U.S. citizen but did not identify the person. The department said in a statement it was in close contact with the Bangladesh government and had offered to help with the investigation.
Bangladesh authorities are yet to say where all the people killed by the militants came from.
The killing of foreigners will likely shatter the confidence of the expatriate community in Bangladesh, many of whom work for multinationals in the country's $26 billion garment industry that accounts for around 15 percent of the economy. Bangladesh is the world's second largest apparel exporter after China.
Thirteen hostages were rescued, including one Japanese and two Sri Lankans, the army said.
Army spokesman Colonel Rashidul Hasan said he could not yet confirm the nationalities of those who had died. Most of them had been killed by "sharp weapons", he said.
Hasan said initially that it seemed all the victims were foreigners but now the army believed some locals were among the dead as well.
Six gunmen were killed during the police operation and one was captured, Prime Minister Sheikh Hasina said in a TV broadcast after more than 100 commandos concluded their operation to clear the cafe. Two police were killed in the initial assault.
"It was an extremely heinous act. What kind of Muslims are these people? They don't have any religion," Hasina said.
Declaring two days of national mourning, she said the country would stand up and fight the "terror threat" that has mushroomed in its backyard.
Islamic State, which has claimed a series of machete attacks on minority groups in Bangladesh over the past year, posted photos of bodies and blood smeared across floors that it said were dead foreigners killed in the assault.
Police did not confirm whether the pictures were from the site of the massacre.
Gowher Rizvi, an adviser to Hasina, told Reuters security forces had tried to negotiate with the gunmen.
The hostage crisis began when security guards in the Gulshan district of Dhaka, popular with expatriates, noticed several gunmen outside a medical center, Rizvi said. When the guards approached, the gunmen ran into a building housing the restaurant, packed with people waiting for tables, he said.
Ali Arsalan, co-owner of the restaurant, said his staff told him the attackers yelled "Allahu Akbar" (God is Great) as they stormed the building, split between Holey Artisan Bakery and the O'Kitchen Restaurant.
The restaurant was regarded as a tranquil place that was safe to visit in an increasingly dangerous city.
Police said the assailants exchanged sporadic gunfire with police outside for several hours after the gunmen attacked the restaurant around 9 p.m. on Friday.
A police officer at the scene said when security forces tried to enter the premises at the beginning of the siege they were met with a hail of bullets and grenades that killed two of them.
A cafe employee who escaped told local television about 20 customers were in the restaurant at the time, most of them foreigners. Some 15 to 20 staff were working at the restaurant, the employee said.
The rescued Japanese man was eating dinner with the seven other Japanese, all of whom were consultants for Japan's foreign aid agency, a Japanese government spokesman said.
Japan's Prime Minister Shinzo Abe said the Japanese aid workers "were giving their all for the development of Bangladesh".
"We feel strong indignation at this inhumane, despicable act of terrorism, which has claimed many lives," he told reporters.
SPATE OF MURDERS
Bangladesh has seen a spate of murders in recent months claimed by Islamic State and al Qaeda on liberals, gays, foreigners and religious minorities.
A Hindu priest was hacked to death on Friday at a temple in Jhinaidah district, 300 km (188 miles) southwest of Dhaka.
Local authorities say no operational links exist between Bangladeshi militants and international jihadi networks. They say two local militant groups, Ansar-al-Islam and Jamaat-ul-Mujahideen, have been behind the violence over the past 18 months. Ansar pledges allegiance to Al Qaeda, while Jamaat-ul-Mujahideen claims it represents Islamic State.
"The bottom line is Bangladesh has plenty of local, often unaffiliated, militants and radicals happy to stage attacks in ISIS's name," said Michael Kugelman, South Asia associate at The Wilson Centre in Washington D.C., using an acronym commonly used for Islamic State.
Islamic State had claimed more attacks in Bangladesh than in Pakistan or Afghanistan, he said.
The restaurant assault also comes after Bangladesh hanged an Islamist party leader, Motiur Rahman Nizami, on May 11 for genocide and other crimes committed during a 1971 war of independence from Pakistan. That has drawn an angry reaction and some scattered violence from supporters.
Foreign diplomats and human rights groups have warned that Bangladesh's ongoing war crime tribunals and the government's pressure on the Bangladesh Nationalist Party have created a backlash domestically,? according to Brad Adams, Asia director at Human Rights Watch.