WASHINGTON — Three United States Army Special Forces were killed and two were wounded on Wednesday in an ambush in Niger while on a training mission with troops from that nation in northwestern Africa, American military officials said.

“We can confirm reports that a joint U.S. and Nigerien patrol came under hostile fire in southwest Niger,” Lt. Cmdr. Anthony Falvo, a spokesman for the United States Africa Command in Stuttgart, Germany, said in an email.

All five American soldiers were Green Berets, said two United States military officials. The attack took place 120 miles north of Niamey, the capital of Niger, near the border with Mali, where militants with Al Qaeda in the Islamic Maghreb, an affiliate of Al Qaeda, have conducted cross-border raids. Niger’s troops were also believed to have suffered casualties, but details were not immediately known.

The deaths represent the first American casualties under hostile fire in a mission in which United States Special Forces have provided training and security assistance to the Nigerien armed forces, including support for intelligence, surveillance and reconnaissance. A Special Forces soldier died in a vehicle accident in Niger in February.