KABUL, Afghanistan  NATO pilots mistakenly attacked Afghan soldiers who had laid a trap for Taliban militants and killed at least five of the soldiers on Wednesday, a devastating case of friendly fire in a conflict still troubled by miscommunication among allied forces, Afghan officials said.

The attack in the Andar district of Ghazni Province, about 100 miles southwest of Kabul, suggested a serious lack of coordination between NATO troops and Afghan forces battling militants who hold sway in part of the district known as Rahim Khiel.

The Afghan soldiers “had made an ambush for the enemy” when they were attacked early Wednesday morning, said Gen. Zahir Azimi, the spokesman for the Afghan Ministry of Defense. He said the “air force” had “bombarded” the Afghan soldiers; a NATO official later said a helicopter had fired a single rocket into the formation of Afghan troops.

“We condemn this incident,” General Azimi said. “Unfortunately this is not the first time this has happened, but we hope this would be the last one.”