Details were murky last night, but the police said a few facts were clear. Three detectives were working on the corner of Kingston and Lefferts Avenue around 9 P.M. in the Wingate section of Brooklyn. The detectives were in civilian clothes, and one had handed over money for drugs to a man identified as Kurt Philip, the authorities said.

The sting went wrong, the detectives said, when Mr. Philip, 18, began running and was chased to the intersection of Kingston and East New York Avenues a block away. Mr. Philip pulled a gun and fired twice at the detective, said Inspector Michael Collins, a department spokesman.

The popping of gunfire could be heard a block away, neighbors in the quiet residential area said. ''I didn't look out the window,'' said a woman who lives on Kingston Avenue and would not give her name. ''It was too close for comfort.''

Two officers from the Queens District Attorney's Warrant Squad were in the neighborhood at the time, rounding up fugitives in an operation unrelated to the drug investigation, Inspector Collins said.

Those officers saw the shooting and chased Mr. Philip back to the corner of East New York and Lefferts Avenues, where they apprehended him, Inspector Collins said.

Mr. Philip, who has two previous arrests for burglary, was being held last night at the 71st Precinct station house in Wingate, Inspector Collins said. A .38-caliber revolver was recovered at the scene, the police said, but it was not clear if it was the weapon used in the shooting.

The authorities were canvassing the area for witnesses or possible accomplices to the crime.

The shooting underscored the dangers in undercover buy-and-bust operations, which are a staple of police efforts to crack down on the illegal drug trade.

In January, a police detective, Sean Carrington, was killed in the Bronx when a drug sting in an apartment hallway erupted in gunfire. Four other officers were shot and wounded in similar incidents between October 1997 and February of this year.

Detective Carrington, too, had not worn a bulletproof vest because he feared it might destroy his cover, Mr. Safir said.

This year, three suspects have been killed by the police in shootouts stemming from undercover drug operations. In most of those incidents, the stings were conducted indoors -- in apartments or vestibules.

Mr. Safir said the outdoor drug operation in the Wingate section had begun a short time ago, after residents complained that their streets were infested with drug dealers.