Five New York City police officers involved in a shootout with a suspect in Harlem in 2005 cannot be held negligent in the wounding of two bystanders, the state’s highest court ruled on Tuesday, ending a five-year legal battle before it went to trial.

In a 4-to-3 decision, the State Court of Appeals found that the five officers were within department guidelines when they returned fire on a robbery suspect who had opened fire on several officers. The suspect was killed, but police bullets also struck a 78-year-old man and a 39-year-old woman who was playing with her 18-month-old daughter.

The woman, Tammy D. Johnson, who was struck in the elbow, sued the department, claiming that the officers had failed to ensure that people on the street were not at unnecessary risk before opening fire. The other person injured, Garnold M. King, who was shot in the lower back, settled a lawsuit with the city last year for $250,000.

Department guidelines say officers may not fire their weapons unless they believe they or other people are in imminent danger of death or serious injury, and if doing so will “unnecessarily endanger innocent persons.”