Seven heavily armed Taliban suicide attackers struck the US consulate in the Afghan city of Herat before dawn on Friday, setting off two car bombs and sparking a shootout with US forces.
The sophisticated attack in the western city, a key business hub near the Iranian border, underscored how the Taliban are able to strike outside their main centres in the south and east.
At least one Afghan guard was killed in the attack and 18 other people wounded, but US officials said there were no US casualties with all consulate staff safe and accounted for.
The US State Department said the attackers appeared to be wearing suicide vests and detonated a truck bomb that "extensively" damaged the front gate.
Afghan security officials said six attackers took part in the assault, detonating a minivan bomb and then a four-wheel drive vehicle, before engaging in a gunbattle with security forces.
The attack comes days after Pakistan said it would soon release its top Taliban prisoner, former military chief Abdul Ghani Baradar, in what Afghan officials hope can ignite peace talks.
The Taliban publicly refuse to negotiate with the Afghan government, branding Afghan President Hamid Karzai a US puppet, but opened a liaison office in Qatar in June billed as a step towards talks with the Americans on a possible prisoner swap.
"Today at around 5:30 am, a minivan suicide bomber detonated his car outside the US consulate in Herat city," Abdul Hameed Hameedi, the deputy security chief of Herat province told AFP.
"Another suicide bomber driving a four-wheel drive detonated outside the first gate of the US consulate -- damaging the gate.
"After that five suicide bombers engaged in a gunbattle with the Afghan security forces, and after a while, all of these suicide bombers get killed in the attack," Hameedi added.
He said an Afghan guard at the US consulate was killed and two other guards and two policemen wounded. A number of civilians were wounded because of the powerful explosion, he said.
Herat hospital spokesman Mohammad Rafiq Sherzai told AFP that 18 people were wounded, including four policemen and 14 civilians.
Abdul Raouf Ahmadi, a spokesman for Herat police, confirmed the same account and said that US and Italian forces were also called to the scene and cordoned off the area.
The Taliban claimed responsibility for the attack, saying that there were several casualties on the Afghan and US side but the militia is known to exaggerate its claims of deaths and injuries.
The front gate of the US consulate was "extensively" damaged, US State Department deputy spokeswoman Marie Harf told reporters.
She said the attack started at 5:30 am when the gunmen, dressed in suicide vests, drove up to the front gate in a truck, opened fire and then detonated the truck bomb.
American and contracted security personnel reacted to the attack before it came to an end, Harf said.
US Ambassador James Cunningham condemned the attack and thanked Afghan and NATO troops for their quick response in securing the building and keeping consulate staff safe.
"We are reminded again of the very real human toll exacted by terrorism. The perpetrators of this attack have shed Afghan blood on Afghan soil," he said in a statement.
Taliban spokesman Qari Yousuf Ahmadi told AFP in a telephone call that the militia were responsible.
"Our mujahedeen, armed with heavy and light weapons, attacked the US consulate in Herat. There are several casualties to Afghan and US forces," he said.
US-led NATO troops who have supported the Afghan government against the Taliban are due to end their combat mission next year, after Afghanistan holds key presidential elections.
The US special envoy for Afghanistan and Pakistan on Tuesday dismissed suggestions that Washington would withdraw all troops from Afghanistan after next year.
Aides to US President Barack Obama earlier this year openly mulled the so-called "zero option" of a complete withdrawal from Afghanistan once US troops end their combat role in 2014.
"If they (the Afghans) don't want anybody, we're not going to stay. But I don't think that's an option the Afghans are likely to choose," James Dobbins, the US Special Representative for Afghanistan and Pakistan, told the Atlantic Council think-tank.
Dobbins said he expected "several thousand American forces and several thousand non-American NATO forces" in 2015 and beyond.
Roughly 100,000 foreign troops now serve in Afghanistan, two-thirds of them from the United States.
Obama has pledged to the war-weary US public to end the country's longest-ever war, which was launched to fight Al-Qaeda and their Taliban allies after the September 11, 2001 attacks.