Gunmen in army uniforms who stormed Kabul's Intercontinental Hotel late on Saturday and battled Afghan Special Forces through the night killed more than 30 people and wounded many more, although the final toll of dead and wounded may still be higher.
Wahid Majroh, a spokesman for the ministry of public health, said that 19 bodies had been brought into city hospitals, with six identified as foreigners.
However a senior Afghan security official, who spoke on condition of anonymity because he was not authorized to talk to the media, said the death toll was over 30 and might climb higher. The dead included hotel staff and guests as well as members of the security forces who fought the attackers.
All five attackers were also killed, interior ministry spokesman Najib Danesh said.
The raid was the latest in a series of attacks that have underlined the city's vulnerability and the ability of militants to mount high-profile operations aimed at undermining confidence in the Western-backed government.
More than 150 guests were able to flee as parts of the building caught fire, with some shimmying down sheets tied together and dropped from upper-floor windows and others rescued by Afghan forces.
Local airline Kam Air said around 40 of its pilots and air crew, many of whom are foreigners, were staying in the hotel and as many as 10 had been killed. Local media reports said the dead included Venezuelans and Ukrainians.
Zamari Kamgar, the airline's deputy director, said it was still trying to locate staff.
The Taliban, which attacked the same hotel in 2011, claimed responsibility for the attack, its spokesman Zabihullah Mujahid said in a statement.
A statement from the interior ministry put the blame on the Haqqani network, a group affiliated with the Taliban that is notorious for its attacks on urban targets.
Abdul Rahman Naseri, a guest who was at the hotel for a conference, was in the hall of the hotel when he saw four gunmen dressed in army uniforms.
"They were shouting in Pashto (language), 'Don't leave any of them alive, good or bad'. 'Shoot and kill them all,’ one of them shouted," Naseri said.
"I ran to my room on the second floor. I opened the window and tried to get out using a tree but the branch broke and I fell to the ground. I hurt my back and broke a leg."
[caption id="attachment_241566" align="aligncenter" width="800"]
This screengrab from an AFP video taken on January 20, 2018, with the aid of a nightvision camera show a member of security forces standing guard in the vicinity of the Intercontinental Hotel in Kabul | AFP
Even after officials said the attack was over, sporadic gunshots and explosions could be heard from the site.
As day broke on Sunday, thick clouds of black smoke poured from the building, an imposing 1960s structure set on a hilltop and heavily protected like most public buildings in Kabul.
The Intercontinental is one of two main luxury hotels in the city and had been due to host an information technology conference on Sunday. More than 100 IT managers and engineers were on site when the attack took place, said Ahmad Waheed, an official at the telecommunications ministry.
Danesh said a private company had taken over responsibility for security at the hotel three weeks ago and there would be an investigation into possible failings, just days after a US embassy warning of possible attacks on hotels in Kabul.
Several armoured US military vehicles with heavy machine guns could be seen close to the hotel along with Afghan police units as Special Forces manoeuvred around the site.
Hotel manager Ahmad Haris Nayab, who escaped unhurt, said the attackers had got into the main part of the hotel through a kitchen before going through the hotel, with many guests trapped in their rooms.
"When the sixth floor caught fire this morning, my roommate told me, either burn or escape," said Mohammad Musa, who was hiding in his room on the top floor.
"I got a bed sheet and tied it to the balcony. I tried to come down but I was heavy and my arms were not strong enough. I fell down and injured my shoulder and leg."
Although US and Afghan officials say the Taliban has come under strain after the United States increased assistance to Afghan security forces and stepped up air strikes against insurgents, security remains precarious across the country.
In separate incidents on Sunday, eight people were killed by a roadside bomb in the western province of Herat and 18 members of local militia forces were killed at a checkpoint in the northern province of Balkh.
As pressure on the battlefield has increased, officials have warned that the danger of attacks on high-profile targets in Kabul and other cities would increase.
After repeated attacks in Kabul, notably last May when a truck bomber killed at least 150 people outside the German embassy, security has been further tightened.
Large areas of central Kabul are already closed off behind high concrete blast walls and police checkpoints but the ability of the assailants to get into a well-protected hotel frequented by both government officials and foreigners demonstrated how difficult it remains to stop them.
The attack, just days after a UN Security Council visit to Kabul to allow senior representatives of member states to assess the situation in Afghanistan, may further weaken confidence in the government.
President Ashraf Ghani ordered an investigation and said militant groups were being helped by neighbouring countries.
"As long as the terrorist groups have secure protection and safe haven, the region will not find security, stability," he said in a statement.
US ambassador John Bass said the embassy was in close contact with Afghan authorities.
"Such violence has no place here or anywhere in the world," he said in a statement.