The NYPD officers who fatally shot hero cop Brian Mulkeen in a tragic case of friendly fire thought they were being shot at when they responded to the scene in the Bronx on Sunday morning, law enforcement sources said.

The five officers shot a total of 10 rounds when gunfire whizzed in their direction as they approached the area of the Edenwald Houses where Mulkeen was struggling with reputed gang member Antonio Lavance Williams, the sources said.

The officers thought Williams was firing at them as they responded — so they began shooting at him, tragically hitting their fellow cop, according to law enforcement sources.

Mulkeen had tackled Williams at about 12:30 a.m. on Sunday after he ran from him and two other plainclothes cops who were patrolling the housing project in an effort to crack down on gang tensions in the neighborhood, authorities said.

As Williams struggled with Mulkeen, the alleged gangbanger reached for the 33-year-old cop’s service weapon, police officials said.

Mulkeen didn’t let go of his weapon during the fight and even managed to squeeze off five rounds at Williams, police said.

It’s unclear if any of the shots fired by Mulkeen hit Williams, but the rounds touched off the volley of gunfire that ended the young officer’s life, sources said.