KABUL — The first shots rang out at 6 p.m. Saturday, just as a small team of U.S. Special Operations forces were leaving a compound in eastern Afghanistan, according to Afghan officials.

“The foreigners started shouting,” said Salim Khan, a police officer who was at his base near the scene. The U.S. troops had been meeting with Afghan army forces at a base in Nangahar province’s Sherzad district just before the attack occurred.

The gunfire lasted just a few minutes, Khan said. The next sound he heard was from the helicopters arriving to evacuate American casualties.

Two U.S. Army Special Forces soldiers were killed and six U.S. troops were wounded in the attack, according to the U.S. military command. The Pentagon on Sunday identified the U.S. soldiers who were killed as Sgt. 1st Class Javier Jaguar Gutierrez, 28, of San Antonio, Tex., and Sgt. 1st Class Antonio Rey Rodriguez, 28, of Las Cruces, N.M.

Gutierrez and Rodriguez were assigned to 3rd Battalion, 7th Special Forces Group (Airborne) at Eglin Air Force Base in Florida, the Pentagon said. Both received posthumous promotions.

The incident is under investigation. Some Afghan officials said the attacker was a member of the Afghan security forces.

Col. Sonny Leggett, a U.S. military spokesman, said the attacker was wearing an Afghan uniform and “opened fire on the combined U.S. and Afghan force with a machine gun.” Leggett said “the cause or motive behind the attack is unknown at this time.”

The Afghan Defense Ministry said only that “an individual opened fire” on U.S. and Afghan forces, killing an Afghan army soldier and wounding three others in addition to the American casualties.

Other Afghan officials, however, confirmed that the attacker’s identity is known and that he was a member of the security forces.

Ajmal Omar, a local official, said the man who opened fire was a soldier originally from Nangahar. A second Afghan official also said the attacker was a member of the military, but it was unclear whether he was a soldier or part of an elite special forces unit. The official spoke on the condition of anonymity because he was not authorized to release details of the incident to the news media.

Omar, the deputy head of the provincial council, said the attacker was killed during the shootout. Omar and Khan, the police officer, said two other soldiers were detained for questioning by the Americans.

U.S. forces put the entire area on lockdown, restricting civilian movement and confining Afghan police officers and army troops to their bases and checkpoints, a local security official said.

Movement in the area was still restricted Sunday morning, said the official, who spoke on the condition of anonymity because he was not authorized to speak to the media.

Khan said helicopters could be heard circling the scene throughout the night into Sunday morning.

Attacks perpetrated by militants who infiltrate Afghan military units have long posed a threat to U.S. and Afghan forces. In 2018, the U.S. military scaled back interactions with Afghan forces because of a rise in insider or “green-on-blue” attacks. An insider attack in 2018 killed a top Afghan regional police commander at a meeting where Gen. Austin “Scott” Miller, the top U.S. commander in Afghanistan, was present.

The U.S. Special Inspector General for Afghan Reconstruction reported that there were seven insider attacks in 2019, more than the year before, but causing fewer casualties.

The Afghan Defense Ministry said Sunday that such attacks “fail to have negative effects on the friendship and spirit of cooperation and between the [Afghan military] and U.S. military forces.”

American forces in Nangahar are fighting both the Taliban and the Islamic State. A large American air campaign supports ground operations carried out by U.S.-backed Afghan forces. The province remains one of the deadliest for Afghan civilians, according to the United Nations.

Peace talks between U.S. and Taliban negotiators remain stalled. They were last called off in December following a Taliban attack on the heavily fortified U.S. air base in ­Bagram.

U.S. negotiators have demanded a reduction in violence before talks can resume and a deal can be signed. Taliban negotiators have offered their American counterparts a violence-reduction proposal.

A roadside bomb attack in Kandahar province last month killed two American service members and left two injured. The Taliban claimed responsibility.

Alex Horton in Washington and Sayed Salahuddin and Aziz Tassal contributed to this report.