At least three people were killed when insurgents attacked the U.S. consulate in western Afghanistan's main city on Friday (September 13), detonating a powerful truck bomb outside the front gates and launching a gunbattle with security forces, officials said.

The attack began at about 6 a.m. (0150 GMT). A witness said he saw flames in front of the compound rising from the wreckage of the vehicle and could hear the gunbattle as the attack unfolded.

The bold attack in Herat, claimed by the Taliban, once again underscored a worrying security picture as Afghanistan prepares to take over from foreign combat troops after 12 years of war and stage crucial presidential elections next year.

While the circumstances of the attack were initially unclear, a spokesman for the U.S. embassy in the Afghan capital of Kabul said all U.S. personnel in the consulate in Herat were safe and had been accounted for.

He described the incident as a "complex" attack that included a car bomb. A U.S. State Department statement later said the attack was over.

Herat police chief General Rahmatullah Safi said a police officer and a translator had been killed and two Afghan staff working in the consulate had been wounded.

Abdul Ahmadi, a spokesman for the main hospital in Herat, later said three people, including two police and a security guard, had been killed and 17 wounded.

Safi said Afghans and Americans had been trapped inside the consulate while fighting raged outside

The U.S. State Department statement said a truck carrying attackers had driven up to the front gate of the consulate and insurgents began attacking Afghan guards and other security contractors. It said the truck later exploded.

The Taliban quickly claimed responsibility for the attack in Herat.

"Our aim for this attack is to show the Americans that they are not safe anywhere in this country," Taliban spokesman Qari Yusuf Ahmadi said in a statement emailed to reporters.