Gunmen armed with machine guns and rocket-propelled grenades attacked the Indian Consulate in western Afghanistan's Herat province, an assault that injured no diplomatic staff, police said.
Indian officials said there had been a threat against its diplomats in Afghanistan, but gave no other details.
At least three gunmen opened fire on the consulate from a nearby home, provincial police chief Abdul Sami Qatra said. Qatra and local police spokesman Raouf Ahmadi said police killed two gunmen.
Gunfire continued into Friday morning. Ahmadi said officers thought there could be a fourth gunmen involved in the fighting as gunfire came from different directions. Police officers surrounding the area had orders to ire carefully due to other residential compounds nearby, Ahmadi said.
By midmorning Friday, the building was ablaze, though it wasn't clear what started the fire. Gen. Taj Mohammed Jahid of the Afghan National Army said soldiers believed the gunmen had hid in the basement. Jahid said soldiers continued to search for the gunmen.
No group immediately claimed responsibility for the attack. Afghanistan is experiencing a rise in insurgent attacks as foreign troops plan to withdraw from the country by the end of the year and as the country faces a coming runoff presidential election.
Syed Akbaruddin, a spokesman for India's Ministry of External Affairs, said a deployment of the paramilitary Indo-Tibetan Border Police at the consulate held off the assault until Afghan forces arrived.
That force is typically responsible for guarding most of India's roughly 4,000-kilometer border with China, much of it running over snowy mountain ranges.
Akbaruddin said all Indians at the consulate were safe.
"Our consulate and our diplomatic presence in Afghanistan have been under threat," Akbaruddin told TimesNow TV, without elaborating.
Herat lies near Afghanistan's border with Iran and is considered one of the safer cities in the country, with a strong Iranian influence. In September 2013, Taliban gunmen launched a similar assault on the U.S. Consulate in the city, killing at least four Afghans but failing to enter the compound or hurt any Americans.
Foreign embassies and consulates remain a favorite target of insurgents in Afghanistan, but many are protected by high walls and multiple gates, as well as security forces.
India has invested more than $2 billion in Afghan projects, including roads and power projects. In August 2013, a botched bombing against the Indian Consulate in the Afghan city of Jalalabad near the border with Pakistan killed nine people, including six children. No Indian officials were hurt. Two attacks on the Indian Embassy in Kabul in 2008 and 2009 that killed 75 people.
Groups known for targeting Indian interests include Lashkar-e-Taiba, which was blamed for the 2008 attack on the Indian city of Mumbai that killed 166 people, and the Haqqani network, which is based in Pakistan's lawless tribal areas along the border with Afghanistan.
Lashkar-e-Taiba has been active in Afghanistan in recent years, often teaming up with insurgent groups operating in the eastern part of the country near the frontier with Pakistan. In 2010, two Kabul guest houses popular among Indians were attacked, killing more than six Indians. India blamed that attack on the group.
Sameer Patil, a national security expert at the Mumbai-based think tank Indian Council on Global Relations, said it was likely that anti-India elements from Pakistan were behind the attack. India and Pakistan have fought three wars since their independence from Britain in 1947, though relations recently have thawed slightly between the two nuclear-armed countries.
"It is likely that by carrying out such violence, anti-India elements in Pakistan may be testing the resolve of India's new government, led by Prime Minister-designate Narendra Modi, on terrorism and how he responds to such acts of violence," Patil in a statement.
The violence comes as the Taliban has launched its annual spring offensive promising to step up attacks against Afghan security forces in a bid to undermine the Western-backed government. The Taliban also have pledged to disrupt voting as Afghans prepare for a second round of presidential elections on June 14.
The first round was relatively peaceful, but no candidate won a majority forcing a runoff vote between the top two candidates - Abdullah Abdullah and Ashraf Ghani Ahmadzai.