An NYPD officer was shot by his partner while the two were trying to arrest a pair of suspects in a heroin investigation in Brooklyn Tuesday night, authorities said.

A detective and a sergeant from Brooklyn North's narcotics unit, both in plainclothes, approached the two suspects in a car at Troutman Street and Wilson Avenue in Bushwick at about 6:15 p.m., police officials said in a news briefing Tuesday evening.

The sergeant and the detective, identified as Jon Gladstone, walked up to the car, one on each side, and identified themselves as cops, officials said. The detective reached into a passenger's side window, and that's when the 45-year-old driver inside the vehicle hit the gas, reversing and hitting hitting a cop car.

Gladstone was still hanging from the car when the suspect drove forward and crashed into another car. Police said that Gladstone and the sergeant fired their shots a total of four times amid the chaos.

"The detective was hit in the shoulder, apparently by the shot that came from his colleague," said NYPD First Deputy Commissioner Benjamin Tucker.

Gladstone was taken to Elmhurst Hospital in Queens, and was said to be awake and alert and "in good spirits." He will undergo further tests at the hospital.

Two suspects were taken into custody; the driver, a 46-year-old man, was shot in the left wrist and left leg. He was taken to Bellevue Hospital and is listed in stable condition. That man, identified as Oscar Vera of Williamsburg, has 58 prior arrests, NYPD Chief of Detectives Bob Boyce said.

He was charged with assault of a police officer, criminal possession of a controlled substance, criminal sale of a controlled substance, reckless endangerment and resisting arrest.

The other suspect, 51-year-old Geraldo Rodriguez, was not hurt. He has been arrested 17 times in the past.

Attorney information wasn't immediately clear for either of the men.

Police had been searching for a third suspect who was allegedly part of the drug transaction, though he was not inside the car at the time. Police said Wednesday he was in custody.

It's not clear if the suspects were armed. Boyce said police have not recovered a gun from inside the car.

Mayor Bill de Blasio visited the detective Tuesday night. He commended the cops for their work in a statement before he arrived at the hospital.

"Tonight’s shooting is an important reminder of the critical and dangerous work our police department does each day," de Blasio said. "Today, a detective put his life on the line as he bravely performed his duty to protect our city and its residents from harm."

He added, "We’re grateful the detective is doing well, and we wish him a safe and swift recovery.”

The officers have not been identified but each had been on the job for 11 years, officials said. The area where they had been staked out was specifically targeted by the narcotics unit for heroin transactions, Boyce said.

"It was very chaotic out here, it was crazy. It looked like a movie to me," witness Raheem Preston said, describing the swarm of police response after the shooting.

Kate Murphy said she saw helicopters flying over the area and she thought, "What's happening to my neighborhood?"

Video posted to social media showed parts of major roads in Queens, including a stretch of Queens Boulevard, cleared as police cars and emergency vehicles rushed to the hospital in the borough.

The shooting marks the sixth time since January an NYPD officer has been shot in the line of duty.

Last month, Officers Andrew Yurkiw and William Reddin were shot during a confrontation with a gunman in Brooklyn. Yurkiw was shot in his bullet-resistant vest and Reddin was struck in the hip.

A little more than two weeks before those two officers were shot, Officers Diara Cruz and Patrick Espeut were shot and wounded while on patrol in a public housing stairwell in the Bronx. The gunman soon afterward killed himself, police said. In January, Officer Sherrod Stuart was wounded in the ankle by a police bullet as another officer exchanged gunfire with a suspect in a Bronx street brawl.

-- Pei-Sze Cheng and Andrew Siff contributed to this report