A 25-year-old man was shot in Brooklyn early Sunday after he allegedly climbed in to an unmarked police car with an officer and sergeant inside, police said.

The man, who was not immediately identified, allegedly jumped in the backseat of the car while it was stopped at a red light near the corner of Avenue D and Nostrand Avenue at about 12:45 a.m. The plainclothes officers were part of the Detective Bureau Grand Larceny Division, police said.

The sergeant, who was sitting in the front passenger seat, and the man began to struggle, police said. The sergeant's gun then went off inside the car and the man allegedly took it and jumped out.

It was not immediately clear why the man allegedly got in the car or took the gun.

A couple of officers nearby then responded to the scene and told the man to drop the gun. But the standoff turned into a shootout with the man allegedly firing the stolen gun and the officers returning fire, police said.

The officer from the unmarked vehicle then got out of the car and starting shooting, striking the man in his torso and lower leg, police said. He was taken to Kings County Hospital in critical, but stable condition.

The officers were not injured, police said.

Charges are pending against the man.