Afghan security forces suppressed a suicide attack on a guest house attached to the Spanish embassy in Kabul, killing three Taliban fighters after hours of intermittent gunfire and explosions that lasted into the early hours of Saturday.
Two foreigners including a Spanish security officer and four Afghan police were also killed in the attack, in a heavily protected part of Kabul close to several embassies and government buildings, Kabul police spokesman Basir Mujahid said.
In addition, nine Afghan civilians were wounded and another 47 Afghans and foreigners were rescued from nearby buildings where they were trapped as security forces sealed off the area around the guest house, which guards at the site said formed part of the Spanish embassy compound.
The latest in a series of attacks on foreign targets in Kabul began at about 6 pm (1330 GMT) on Friday when a suicide attacker detonated a car bomb near the guest house, allowing three gunmen to take up positions and open fire on security forces.
"The operation took time because we wanted to rescue the people trapped in surrounding buildings and we had to move cautiously and in a proper tactical manner," Kabul police chief Abdul Rahman Rahimi told Reuters.
The Taliban claimed responsibility for the attack on the "invader's guest house", just days after President Ashraf Ghani returned from a regional peace conference in Pakistan, where he sought support to revive peace talks that stalled this year.
Ghani's government has come under increasing pressure as the Taliban insurgency has spread since foreign forces ended combat operations last year.
The head of the intelligence services, who strongly opposed Ghani's bid to involve Pakistan in the peace process, resigned on Thursday, underlining divisions in the national security apparatus.
In a statement issued on Saturday, the Taliban taunted authorities with the "disgrace" of not being able to prevent an attack in the heart of the capital.
"The presence of our Mujahideen with weapons and a car loaded with explosives in such a high security area shows God’s support and the cooperation of the poor and Muslim people," spokesman Zabihulla Mujahid said in a statement.
The Islamist movement has been racked by internal power struggles of its own with rival factions battling for supremacy since it confirmed in July that its founder, Mullah Mohammad Omar, had died more than two years previously.
However, that has not prevented the militants from gaining a string of successes, including the brief seizure of the northern city of Kunduz in September.
Friday's attack followed a separate Taliban attack on the airport complex in the southern city of Kandahar, in which at least 50 civilians and security forces personnel were killed.
The Taliban are fighting to expel foreign forces and bring down the Western-backed government.