The attack in the city of Herat underscored concerns about an insurgency that shows no signs of letting up as U.S.-led troops reduce their presence ahead of a full withdrawal next year.
Within hours of the assault, the U.S. temporarily evacuated many of its consular personnel to the embassy in Kabul, 650 kilometers (400 miles) to the east.
Herat lies near Afghanistan's border with Iran and is considered one of the safer cities in the country, with a strong Iranian influence. Friday's attack highlighted the Taliban's reach: The militants once concentrated their activities in the east and the south, but in recent years have demonstrated an ability to strike with more frequency in the once-peaceful north and west.
In a phone call, Taliban spokesman Qari Yousef Ahmadi took responsibility for the assault.
An interpreter and three members of the Afghan security forces were killed, said U.S. State Department deputy spokeswoman Marie Harf.
Seven militants, including two drivers of explosives-laden vehicles, also died, according to Gen. Rahmatullah Safi, Herat province's chief of police.
At least 17 people were wounded, said Herat hospital official Sayednaim Alemi.
The attack began about 6 a.m. when militants in an SUV and a van set off their explosives while others on foot fired on Afghan security forces guarding the Consulate, Safi said.
He said the militants were not able to breach the compound, where Americans live and work.
Harf said the attackers fired rocket propelled grenades and that the compound's front gate was extensively damaged in one of the bombings.
Footage broadcast on Afghanistan's Tolo television network showed Afghan police dragging away a badly bloodied man from the scene. Rubble and twisted pieces of metal lay strewn in a seemingly wide area near the consulate.
American security personnel were among those responding to the attack, Harf said.
Robert Hilton, a spokesman for the U.S. Embassy in Kabul, said "all consulate personnel are safe and accounted for." Most of the staff were temporarily relocated to the capital, but some essential personnel stayed in Herat, he said.
U.S. and other foreign missions are attractive targets for militants in Afghanistan, but their high walls and strict security also make them difficult to penetrate. The militants also often carry out complex attacks that include suicide car bombers and fighters on foot.
Last month, a botched bombing against the Indian Consulate in the Afghan city of Jalalabad killed nine people, including six children. No Indian officials were hurt. And two years ago to the day, insurgents fired rocket-propelled grenades and assault rifles at the U.S. Embassy, NATO offices and other buildings in Kabul.
Also Friday morning, a suicide truck bombing wounded seven Afghans in eastern Paktika province's Sar Hawza district, said Mokhlis Afghan, a spokesman for the provincial governor. Paktika province lies along the border with Pakistan, and militants affiliated with the Taliban and al-Qaida are active in the region.
Friday's attacks came in the wake of nationwide celebrations after the Afghan soccer team won the South Asian Football Federation Championship on Wednesday. The win produced a rare moment of national unity in this ethnically divided country, and euphoric Afghans poured into the streets to express their joy over the victory.
AP National Security Writer Lara Jakes in Washington contributed to this report.