“He is in good spirits,” Benjamin B. Tucker, first deputy commissioner of the Police Department, said during a news conference at the hospital. “For someone who’s been shot, he was awake, he was alert.”

One person taken into custody, a 45-year-old man who was behind the wheel, was hit in the wrist and the leg by gunfire, the police said; he was taken to Bellevue Hospital Center, but his condition was not immediately available. Another man, 51, was also taken into custody. A search for a third person at the scene of the transaction, which included $80 worth of heroin, continued late on Tuesday. The police said the brand of heroin was called American Dream.

Both of the men in custody, whose names were not released, have extensive criminal histories, the police said. The 45-year-old man had 58 prior arrests, many of which were related to drug trafficking, the authorities said.

No gun was found at the scene.

“Today, a detective put his life on the line as he bravely performed his duty to protect our city and its residents from harm,” Mayor Bill de Blasio said in a statement. “We’re grateful the detective is doing well, and we wish him a safe and swift recovery.”

In Bushwick, in an area where streets are lined with apartment buildings, bodegas and bars, many residents were startled by the sounds of gunfire and crashing cars ringing out during the evening rush.