Then-Lt. Gen. John W. Nicholson Jr. during his confirmation hearing in January 2016 before becoming commander of U.S. forces in Afghanistan. “Anytime we lose a member of our team, it is deeply painful,” he said Wednesday. (Mark Wilson/Getty Images)

Two Americans were killed and another three were ­injured in a rare attack on foreign troops in the Afghan capital Wednesday, U.S. and Afghan ­officials said.

A gunman fired on international advisers at an ammunition depot near Camp Morehead, a training site for Afghan commandos, about six miles south of Kabul.

The attack, which took place near the entrance of the base, killed one U.S. service member and injured another. One U.S. ­civilian was also killed, and two more were wounded in the ­assault, a statement from the ­NATO-led coalition said.

The gunman, who according to the Afghan Defense Ministry was wearing an Afghan army uniform, was killed after international troops responded with gunfire. The injured Americans remained in ­stable condition, NATO said.

“It happened near an arms depot. The attacker opened fire on his [coalition] friends,” Defense Ministry spokesman Dawlat ­Waziri said.

Elite Afghan commandos crouch during a lesson at Camp Morehead in Afghanistan in February. (Tim Craig/The Washington Post)

A U.S. defense official, speaking on the condition of anonymity ­because the situation was still ­developing, said the Americans were at the depot as part of the NATO training mission for Afghan security forces.

The United States supplies the NATO mission with about 6,800 troops to advise and assist Afghan soldiers who are battling a fierce Taliban insurgency in key areas across the country. An additional 3,000 U.S. troops are dedicated to Operation Freedom Sentinel, a separate counterterrorism mission focused on al-Qaeda and the Islamic State.

The U.S. official said it was not clear Wednesday whether the ­assailant was in fact a member of the Afghan National Army. But Wednesday’s shooting appeared to be what is known as an “insider” or green-on-blue attack, where ­Afghan allies turn on their U.S. and foreign mentors.

At least 150 coalition troops have been killed in such attacks since 2008, according to data ­compiled by the Long War ­Journal, which is published by the D.C.-based Foundation for the ­Defense of Democracies.

The assaults — which have been attributed both to Taliban infiltration and cultural differences between Afghan and foreign troops — surged from 2011 to 2013. But the attacks became more infrequent as the bulk of U.S. and NATO troops withdrew from the country. U.S. forces have suffered only two combat casualties in Afghanistan this year, both of which occurred in the volatile Helmand province in the country’s south.

At Camp Morehead, elite Afghan commando units learn ambush tactics and how to call in airstrikes, and they train for short missions. But the commandos, which number about 11,000, are increasingly stretched thin.

As Afghan forces struggle to beat back Taliban militants in places like Lashkar Gah and Kunduz, which was briefly taken by insurgents again this month, casualties and desertions have ­depleted their ranks.

“Any time we lose a member of our team, it is deeply painful,” Gen. John W. Nicholson, commander of U.S. forces in Afghanistan, said Wednesday in a statement. There were no further details on the two Americans who were killed.

More than 2,350 U.S. troops have died supporting military ­operations in Afghanistan since 2001.

Gibbons-Neff reported from Washington. Sayed Salahuddin in Kabul contributed to this report.