Afghan Taliban insurgents attacked 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 was 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.
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 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.
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 said a huge truck bomb damaged the outer defenses 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.
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.
Earlier this month Taliban targeted the Torkham US base in eastern Afghanistan, sparking a lengthy gun battle in which three insurgents were killed.