Police say the detective agreed to buy crack cocaine from the 17-year-old suspect on Thursday in what's known as a "buy and bust." It happened inside an apartment building near Crotona Park.
After handing over the drugs, police say the suspect pulled a .25-caliber gun and demanded more money.
Police say the suspect fired once at the detective at close range. The detective let off four rounds, striking the suspect in the torso. He was pronounced dead at Lincoln Hospital. The detective was not injured.
The suspect's name was not immediately released.