Police said at least 13 hostages, including three foreigners, had been rescued after commandos took control of the cafe in the upmarket Gulshan quarter but added that there had been an unknown number of casualties.
Two police officers were also killed at the start of the siege as they battled the hostage-takers who were heavily armed with explosives.
The Islamic State organisation claimed responsibility for the attack which follows a series of gruesome killings of religious minorities, foreigners and liberal activists in the mainly Muslim nation.
“The operation is over. The situation is completely under control,” army spokesman Colonel Rashidul Hasan told AFP.
Tuhin Mohammad Masud, a commander of the elite Rapid Action Battalion which led the storming operation, said the cafe itself had been cleared but added that some of the hostage-takers might still be at large.
“We have gunned down six of the terrorists” Masud told India´s Times Now television channel.
“The main area that they have been occupying has been cleared… Maybe some of them have escaped into a larger area.”
There was no official word on the number of hostages who had been killed or wounded but Masud said that “obviously there have been casualties”.
An AFP photographer at the scene said he could hear a massive gunfight as security forces launched the rescue operation more than 10 hours after militants seized the hostages on Friday night.
Eight hostages including a foreigner were rescued in the first few minutes of the operation, a senior security official told AFP on condition of anonymity.
TV footage showed ambulances rushing some of those who had been rescued to a military hospital.
Sri Lanka’s government said two of its nationals were among the hostages rescued and were safe and unharmed.
The Islamic State group claimed responsibility for the attack on the Holey Artisan Bakery cafe in Gulshan which is Dhaka´s wealthiest neighbourhood and home to many embassies.
Police said the gunmen burst into the restaurant shouting “Allahu Akbar” (God is Greater) as people were having dinner at around 9:20 pm and set off explosives.
Italy’s ambassador Mario Palma told Italian state television seven Italians were among the hostages. Japan said some of its nationals may also have been among the captives.
Some diners managed to escape including an Argentine chef and a Bangladeshi man who took refuge in an adjacent building.
Whole building was shaking
Some managed to speak to relatives by phone, reporting there were up to 40 people trapped inside, around half of them foreigners.
Another had told relatives he feared they would be killed if police tried to storm the restaurant to end the siege.
“He is very nervous,” the man’s uncle, who had spoken to him by phone, told AFP.
The restaurant´s supervisor Sumon Reza who escaped by jumping from the roof told a local newspaper there were 20 foreigners being held hostage.
“I was in the roof. The whole building was shaking when they set off explosives,” he said.
Bangladesh has been reeling from a wave of murders of religious minorities and secular activists by suspected Islamist militants.
But those murders generally only involved a handful of assailants while the latest attack appears to have been on a much bigger scale and the first time that people were held hostage.
Popular with foreigners
The IS-linked Amaq news agency said the group was behind the attack and that “more than 20 people of different nationalities (were) killed”.
It later issued a number of photographs of what it said were scenes from inside the cafe.
The pictures, which were not immediately possible to verify, showed what appeared to be a number of bodies lying in pools of blood.
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 firefight with police.
“Two police officers including the head of Banani police station were killed. It appeared they were hit by bullets and splinters from a grenade,” deputy commissioner of Dhaka police Sheikh Nazmul Alam told AFP.
“Up to 20 police officers were injured.”
The attack, which comes as Bangladesh observes the Muslim holy month of Ramazan, 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.
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.