The attack, claimed by Islamic State, 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 mostly Muslim Bangladesh.
Six gunmen were killed during the police operation and one was captured, Prime Minister Sheikh Hasina said in a TV broadcast.
All 20 victims were foreigners, the spokesman for the Bangladesh army said. Colonel Rashidul Hasan said he could not yet confirm the nationalities of those who had died, most of whom were killed by "sharp weapons".
Many people in the expatriate community in Bangladesh work in the country's $25 billion garment sector.
The army concluded an operation to clear the cafe after a 12-hour siege that began when gunmen stormed the restaurant on July 1 night. Two police were killed in the initial assault.
The 13 hostages that were rescued included one Japanese and two Sri Lankans, the army said.
One Japanese man was among those rescued and taken to a Dhaka hospital with a gunshot wound, a Japanese government spokesman said. Seven Japanese were unaccounted for.
An unknown number of Italians were among the hostages who were killed, a source at Italy's foreign ministry said on Saturday. Seven Italians were in the cafe when the attack started, including several working in the garment industry, Italian media have reported.
Islamic State posted photos of what it said were dead foreigners killed in the assault.
Gowher Rizvi, an adviser to Hasina, told Reuters security forces had tried to negotiate with the gunmen.
Sporadic gunfire
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 that his staff told him the attackers yelled "Allahu Akbar" (God is Great) as they stormed the building that is split between a bakery and the O'Kitchen Restaurant.
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 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 that killed at least two of them.
Television footage showed a number of police being led away from the site with blood on their faces and clothes.
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 seven other Japanese, all of whom were consultants for Japan's foreign aid agency, the Japanese government spokesman said. He did not know what happened to the others.
Spate of murders
The hostage crisis marks an escalation from a recent spate of murders 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.
Both Islamic State and al Qaeda have claimed responsibility for many of the killings, although local authorities say no operational links exist between Bangladeshi militants and international jihadi networks.
Bangladesh security officials say two local militant groups, Ansar-al-Islam and Jamaat-ul-Mujahideen, have been behind the spate of 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. Nizami, 73, was a former legislator and minister during opposition leader Khaleda Zia's last term as prime minister.
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.
"They need to maintain legal political space for Jamaat and the BNP so that they don't drive people into the shadows and violence," Adams said in a telephone interview, cautioning that it's not known whether that dynamic and the bloodshed in Dhaka were related.