An on-duty US Immigration and Customs Enforcement officer shot a man in the face in Brooklyn on Thursday morning after an attempted deportation went awry, authorities said.

Officers from ICE’s Enforcement and Removal Operations team — who are in charge of arresting and deporting illegal immigrants — were in the process of serving a deportation order against Gaspar Avendano-Hernandez on West 12th Street near Highlawn Avenue in Gravesend when an associate of the deportee “physically attacked” the arresting officers, according to an ICE spokeswoman and law enforcement sources.

In the midst of the scuffle, the officer’s service weapon went off, sending a bullet through 26-year-old Erick Diaz’s hand that then ricocheted into his face, according to sources and Diaz’s brother, Kevin Yanez Cruz.

An ICE source told The Post that Diaz had reached for the officer’s gun, which is why he was shot.

Diaz suffered non-life-threatening injuries and was transported to Maimonides Hospital in serious condition, the FDNY said.

He was also arrested for interfering with the deportation, sources said.

Avendano-Hernandez, with one handcuff on his wrist, used the chaos of Diaz’s gunshot wound as an opportunity to run back into his house and barricaded himself inside until the NYPD came and arrested him, sources said.

The deportee is a “twice-removed illegal alien from Mexico” who was convicted of assault in 2011 in the Big Apple, according to ICE.

He was arrested by the NYPD on Feb. 3 for possessing a forged instrument, a felony charge, and ICE attempted to “lodge an immigration detainer” against him following the arrest, the agency said.

“However the subject was released from local custody before ICE could lodge a detainer. This forced ICE officers to locate him on the streets of New York rather than in the safe confines of a jail,” ICE said in a statement.

Cruz, 19, who said he was present for the incident, told reporters at the scene Avendano-Hernandez is his mom’s boyfriend and ICE officers tackled him around 7:30 a.m. as he went inside his house to get a tool for work that he’d forgotten.

“These dudes came out of nowhere and tackled the s–t out of him. They didn’t say, ‘We’re here to arrest you, take you into custody.’ They didn’t say nothing. They had handcuffs and guns, but they didn’t identify themselves as officers. And then he reacted. He started to fight back,” Cruz told The Post, adding that his mom’s beau was also tased.

After his brother was shot, Cruz said he fell to the ground and put his hands on his face.

“I thought I was going to lose him. I’m f–ked up over it. It was terrible. I feel nauseous, I feel dizzy. The gun was literally right [at my ear]. Everywhere was an explosion when I heard that gun go off,” Cruz said.

ICE said they first encountered Avendano-Hernandez on Rikers Island in January 2011 and served him an appearance notice for immigration court, where he was ordered removed by a judge that March and deported shortly after.

The agency encountered him again about a month later and deported him a second time, although it’s unclear where they came into contact with him the second time.

He’s now back in ICE custody “after attempting to obstruct his arrest,” the spokesperson said.

Cruz is a US citizen and Diaz is here on a 10-year visa, according to Cruz.

Additional reporting by Joe Marino