KABUL — An Afghan soldier opened fire on U.S. troops in a restive eastern province of Afghanistan on Saturday, killing two and injuring at least two more, authorities said.

The shooting occurred in the Achin district of Afghanistan’s Nangahar province, according to a U.S. defense official, an area where both Islamic State and Taliban insurgents are contesting territory.

Early media reports suggested the assailant was an elite Afghan commando, although that is not confirmed. The gunman was killed by American troops, according to a security official in the province.

Achin has been the site of heavy fighting in recent months as U.S. Special Operations forces — including elite Army Rangers and Green Berets — have been working alongside Afghan commandos to route the Islamic State from the area. Three U.S. soldiers had died there this year before Saturday’s shooting.

The Taliban are also active in the area, and there have been reports of clashes between the two insurgent groups in recent weeks. A Taliban spokesman sent a text message to journalists Saturday claiming the alleged assailant was an “infiltrator” of the Afghan army.

Allied commanders provided limited details.

“We are aware of an incident in Eastern Afghanistan. We will release more information when appropriate,” Douglas High, the public affairs officer for Operation Resolute Support, the NATO mission in Afghanistan, said in an email statement to The Post.

Also Saturday, two Afghan border policemen were killed by U.S. aircraft fire during a joint operation in the southern province of Helmand. The U.S. military in a statement apologized for the deaths and said the incident was under investigation.

The three previous U.S. soldiers deaths in Achin make up the entirety of U.S. combat fatalities in Afghanistan in 2017. In early April, Staff Sgt. Mark R. De Alencar, 37, was killed by small-arms fire, followed by Sgt. Joshua P. Rodgers, 22, and Sgt. Cameron H. Thomas, 23, at the end of the month.

Rodgers and Thomas were killed during a joint Afghan-U.S. nighttime raid on an Islamic State headquarters building. The Pentagon is investigating if they were mortally wounded by friendly fire. The raid resulted in the death of the emir of the Islamic State’s branch in Afghanistan, Abdul Hasib, according to the Pentagon and Afghan officials. More than 30 other militants were also killed.

Achin was the also site where U.S. Special Operations troops in April dropped the GBU-43, a 22,000-pound bomb known as the MOAB, on a purported cave complex where insurgents were believed to be hiding. The blast flattened a swath of the countryside. While Afghan officials said dozens of militants were killed, the Pentagon has remained mum on what exactly the bomb accomplished.

The Afghan branch of the Islamic State, known as ISIS-K, is mainly composed of militants pulled from other groups and has turned into one of the main counterterrorism efforts for the United States in Afghanistan. Although military officials say the group is far smaller than it was at its height in 2015, an estimated 600 to 800 militants, located mainly in remote mountainous areas, continue to pose an ample threat to U.S. and Afghan troops.

Afghan President Ashraf Ghani surprised everyone at a peace conference on Tuesday when he said that up to 11,000 foreign fighters have penetrated Afghanistan, a number far higher than previous estimates by the U.S. military and his own government.

The deaths come as the Trump administration is weighing whether to send more U.S. troops to Afghanistan, including a proposal to send American U.S. Special Operations forces to train up to 17,000 new members of Afghan special forces. The Afghan special forces units have quickly become the most reliable force in the more than 15-year-old war, American officials believe. Once trained to conduct raids much like their U.S. counterparts, the majority of Afghan commando units have turned into a backbone force for the fledgling Afghan army.

The deteriorating security situation in Afghanistan was underscored by a massive truck bomb blast on May 31 in a heavily patrolled diplomatic area of the capital Kabul, that killed more than 150.

The bomb blast was one of the worst such attacks in the country since the arrival of American troops in 2001. That was followed by violent anti-government protests that left six dead on June 2 and a triple-suicide bombing during funeral prayers for one of the protesters on the following day.