A plainclothes narcotics detective was shot by friendly fire Tuesday when a suspect tried to drag him with a van during a drug sting in Brooklyn, cops said.

Detective Jon Gladstone was hit in the left shoulder by another cop’s bullet at 6:15 p.m. at Wilson Avenue and Troutman Street in Bushwick, according to police.

“I heard a guy screaming, ‘I’ve been shot! I’ve been shot! Call an officer, I’ve been shot!’ ” said Bri Brown, who works at Miles, a nearby bar. “I saw people running, so I poked my head out . . . They were running away, past the bar, all the people on the street.”

The detective was recovering at Elmhurst Hospital in Queens, where he was alert and talking.

He and a sergeant had witnessed an $80 drug buy made by undercover officers. After the deal was done, the duo moved in to make an arrest.

As they tried to bust two of the suspects inside a van, the driver pulled away with the detective’s arm still in the vehicle.

Gladstone and the sergeant both opened fire on the vehicle. They fired four shots, striking the driver in the leg and wrist.

The driver, 46-year-old Oscar Vera, was taken to Bellevue Hospital in stable condition.

A second suspect in the vehicle, Geraldo Rodriguez, 51, was taken into custody, sources said.

A third suspect, who was standing outside the van during the shooting, was being sought by authorities.

The arrested suspects were unarmed, and it appeared the detective was struck by the sergeant’s gunfire, cops said.

Mayor Bill de Blasio said Gladstone was “in good spirits and very alert.”

The driver, Vera, has 58 prior arrests, mostly for drug-related offenses.

Residents said cops were swarming the neighborhood.

“It’s bonkers over here. It’s like ‘1984’ — really ominous,’’ said Kaitlin Kaufman, a 25-year-old actor who lives nearby.

Additional reporting by Dana Sauchelli and Gabrielle Fonrouge