Gunmen stormed the Holey Artisan Bakery cafe late on Friday before troops entered almost 13 hours later.
Six of the attackers were also killed and one was arrested, a government spokesman said.
The rescued included two Sri Lankans and a Japanese citizen who was wounded, Lt. Col Tuhin Mohammad Masud said, adding there were casualties among the other hostages. Japan confirmed that one of its citizens had been rescued and said seven were still unaccounted for.
Daesh (ISIS/ISIL) terrorist group claimed responsibility for the attack.
“We’ve recovered 20 bodies. Most them had been brutally hacked to death with sharp weapons”, Brigadier General Naeem Ashfaq Chowdhury told reporters, without giving the nationality of the victims.
Seven Italian nationals are believed to have been in the cafe, the Italian ambassador in Dhaka, Mario Palma, was quoted as saying by Italian media.
Italian news agency Ansa said most victims were from Italy and Japan – Italian media said many of those dining at the cafe worked in the garment industry.