A plainclothes anti-crime cop was shot and wounded early Saturday morning, four years to the day he was appointed to the NYPD, by a suspect who police said had been released from court only hours before.

Officer Sherrod Stuart, 25, was with a partner at around 2 a.m. responding to a report of a street fight with guns, bats and knives in the South Bronx when the pair encountered a suspect and pursued him, according to an NYPD release.

As backup arrived, a gunfight ensued and Stuart was shot in the right foot. He returned fire and hit the man, identified as 19-year-old Christopher Rice, four times, according to the NYPD.

Stuart was in stable condition at Lincoln Hospital. Rice was in serious condition at the same hospital.

Rice charged on Monday with assault on a police officer, menacing and criminal possession.

Rice has five prior arrests, including one on Friday for fare beating, police said. He was released at 11 p.m., just a few hours before the shooting.

Five stabbing victims from the street fight that prompted the initial 911 call were also taken to nearby hospitals.