Media playback is unsupported on your device Media caption Several police and civilians were reportedly wounded in the attack

US and Afghan forces have fought off an attack by Taliban insurgents on the US consulate in the western city of Herat.

Two Afghan police and one security guard were killed in the dawn assault, along with seven attackers. The US consulate said its staff were safe.

The Taliban said they carried out the attack, which began with a huge blast at the compound gates, sparking a gun battle near consulate buildings.

Attacks continue despite the planned withdrawal of foreign troops in 2014.

Seventeen civilians, including women and children, were injured in the Herat attack.

Analysis Herat city may usually be peaceful, but the Taliban are able to operate in the countryside nearby, and the US consulate, standing in a tall former 5-star hotel was a tempting target. The attack followed a familiar pattern of a suicide explosion followed by other attackers. In the week of the anniversary of 9/11, security forces were expecting the Taliban to attempt a 'spectacular' attack. The success of Afghan forces in repulsing a determined assault is a relief. Both the Afghan police and army are far better than they were, and Afghan police did most of the fighting in Herat, with US troops backing them up. Once again Afghan police bore the worst casualties. But 12 years after the Taliban government was ousted, their ability and willingness to carry out attacks of this sort asks serious questions about the international strategy in Afghanistan.

Herat sits close to the Iranian border on a vital trade route across southern Afghanistan and it has been relatively peaceful in recent years.

The US consulate in Herat was opened just four years ago in a former five-star hotel.

The BBC's David Loyn in Kabul says this attack - carried out in the shadow of the anniversary of 9/11 - is a demonstration of the ability of insurgents to disrupt Afghanistan, 12 years after US-led forces toppled the Taliban.

In violence elsewhere, a truck bomb exploded near government offices in the volatile south-eastern province of Paktika, injuring several Afghan security personnel.

'Extensive damage'

The force of the explosions in Herat left the road badly damaged and pieces of twisted metal and rubble could be seen in the vicinity of the consulate, reports say.

An Afghan army spokesman told the BBC that a huge truck bomb damaged the outer defences of the US consulate, allowing the attackers to breach the perimeter and shoot at the consulate buildings.

"A truck carrying attackers drove to the front gate, and attackers, possibly firing rocket-propelled grenades and assault rifles, commenced attacking Afghan Protective forces on the exterior of the gates," US State Department spokeswoman Marie Harf told reporters.

"Shortly after, the entire truck exploded, extensively damaging the front gate [to the consulate]," she added.

Image caption The US consulate in Herat, once home to a five-star hotel, was the target for the Taliban attack carried out in the shadow of the 9/11 anniversary Image caption The attack began shortly before dawn when a vehicle exploded close to the gate of the compound Image caption The explosions sparked a gun battle. Witnesses said they could see smoke rising from the scene Image caption Two policemen and one security guard were killed - the police bore the brunt of this particular assault Image caption The force of the explosions left the road badly damaged and pieces of twisted metal and rubble could be seen in the vicinity of the consulate Image caption Herat is considered to be one of the more peaceful cities in Afghanistan. This attack underscores the challenge that awaits the country in 2014 previous slide next slide

The gunfire continued for some time, reporters at the scene said.

The Nato-led International Security Assistance Force (Isaf) tweeted at 04:00 GMT that the consulate was secure "with all enemy forces being killed". Isaf and Afghan National Security Forces were at the scene, it added.

Peter Bezkorowajnyj, who works in Afghanistan, told the BBC he could see smoke from the explosion from the roof of his building.

The explosion "rocked the building I am staying in", he said.

Earlier this month Taliban targeted the Torkham US base in eastern Afghanistan, sparking a lengthy gun battle in which three insurgents were killed.