Nato helicopters were called in to kill three militants to help end a five-hour clash by suicide bombers and gunmen on a hotel in the Afghan capital, Kabul.
The attack on the Intercontinental Hotel, frequented by Westerners, left six more attackers, two police and 11 civilians, including a Spaniard dead.
A security official said the militants may have exploited renovation work to gain access to the high-security hotel.
A Taliban spokesman said the insurgent group had carried out the attack.
However, interior and defence ministry officials told the BBC it bore the hallmarks of the Haqqani network, a group closely allied to the Taliban but which operates independently.
President Hamid Karzai condemned the attack, saying that the insurgents enjoyed spilling innocent blood but that such incidents would not hinder the process of transition of responsibility for security from Nato-led to Afghan security forces.
Smoke and flames could be seen coming from the hotel as the sun rose over Kabul.
Afghan security officials said at least nine militants had stormed the building, and all had been killed.
A senior provincial judge, some waiters and musicians performing at the hotel were among the dead.
At least eight people were wounded including two New Zealand members of Isaf special forces who were "moderately" injured.
The attack began while many guests were in the dining room late on Tuesday.
Afghan officials told the BBC that one suicide bomber had blown himself up at the front of the hotel and another on the second floor.
Witnesses said panic broke out as guests, including a wedding party, fled for safety.
'Hiding in hotel room'
An official from the northern Takhar province told the BBC he saw men armed with grenade-launchers and machine guns running towards the hotel.
They shot dead his friend, a senior judge from the province, Mawli Hamdullah Warsaj added.
Afghan troops and police sealed off the building and cut the power, as security forces were using night-vision equipment.
Intense gunfire was heard coming from the hotel and some explosions could be heard up to 5km (3 miles) away, said the BBC's Bilal Sarwary in Kabul.
Afghan police said one militant was shot dead as security forces fought their way through.
Three attackers managed to reach the roof and Afghan officials then asked the Nato-led International Security Assistance Force (Isaf) for help, security sources told the BBC.
Isaf spokesman Major Tim James said those killed on the roof by Nato helicopters appeared to have been wearing suicide vests.
"We've had reports that there were a number of explosions caused either by the insurgents detonating themselves or the engagement by the helicopter causing [suicide vests] to explode," he told AFP news agency.
Kabul police chief Gen Ayub Salangi said that one of the attackers managed to survive the assault by hiding in a hotel room.
He detonated his explosive vest at around 0700 local time (0230 GMT), after the fighting was thought to have ended, killing two policemen and a Spanish guest, and injuring three others, the police chief said.
The Spaniard had tried to return to his room, against the advice of security forces, he added. Spain's foreign ministry later confirmed that a Spanish citizen had died, saying that he was an airline pilot.
An Afghan official said the attackers had hand-grenades, rocket-propelled grenades and AK-47s.
'Target of attack'
Gen Salangi said his own armoured land cruiser had been hit more than 30 times, and its windows were broken.
Officials said a meeting of provincial governors taking place at the hotel might have been the target of the attack.
The attack also came the night before the start of a conference about the security transition.
Correspondents say the Intercontinental, which is not part of the international hotel chain of the same name, is one of Kabul's most heavily guarded hotels.
But a security ministry official told the BBC that the militants could have exploited a loophole in security caused by renovation work.
"The insurgents are using every means to infiltrate into tight security areas. They might have camouflaged themselves as labourers, as technicians or whatever."
The US state department condemned the attack, saying it demonstrated "the terrorists' complete disregard for human life".
Kabul - the scene of many attacks over recent years - has been relatively stable so far this year, although violence has increased across the country since the killing of Osama Bin Laden in Pakistan on 2 May, and the start of the Taliban's "spring offensive".
In January 2008, militants stormed the capital's most popular luxury hotel, the Serena, and killed eight people, including an American, a Norwegian and a Philippine woman.