The incident began shortly before 1 a.m. near the intersection of Western and Florence avenues, according to the Los Angeles Police Department.
Officers had pulled over a vehicle when an unidentified man, who was not involved in the traffic stop, approached them and started shooting in their direction, the LAPD said.
Two of the three officers at the scene returned fire, striking the gunman, investigators said. The suspect was pronounced dead at the scene.
No officers were injured in the confrontation. A 9-millimeter "ghost gun" was recovered, the LAPD said.
After the shooting, five people were detained at an illegal marijuana dispensary nearby and subsequently arrested. Police initially said six people were arrested.
Officers believe that the suspect didn't know police were there, and that his possible target may have been the marijuana dispensary, which appeared to have been operating inside a clothing store called Grind Til We Rich.
"That is one of possibilities, it could be a connection there," LAPD Cmdr. German Hurtado said.
Further examination of video from nearby places and body-worn cameras will help police determine the suspect's target, Hurtado said.
Whether the suspects in custody had any connection to the gunman was unclear.
The driver involved in the traffic stop was neither armed nor detained, authorities said.