An NYPD officer shot in the foot while breaking up a melee in the South Bronx was struck by friendly fire, police said Monday. 

The police said the shooting remained under investigation. 

The shooting, in which police critically wounded the gunman, happened as the officer, Sherrod Stuart, responded to “a very large ‘jump-up’ party with anywhere from 100 to 200 people,” Police Commissioner William Bratton said Sunday at a pre-dawn news conference at Lincoln Medical Center in the Bronx, the hospital where Stuart was recovering.

Stuart, 25, whose four-year anniversary with the NYPD was Saturday, was patrolling in plainclothes with a partner when they were dispatched about 2:10 a.m. to reports of a large fight with guns, bats and knives in front of 2505 Third Ave., near the scene of an unlicensed club hosting the party, Bratton said.

Five people were also hospitalized after being stabbed during the brawl to which police were responding, but a police spokeswoman said Saturday afternoon that she did not know the extent of their injuries.

Just before the gunfire, Stuart and the partner were chasing a suspect who began to shoot, Bratton said. Stuart was shot in the right ankle and shot the man they were chasing, Christopher Rice, 19, four times, Bratton said.

Rice was also brought to Lincoln hospital, where he was in critical condition; and Stuart was in stable condition, police said.

“What an impressive young man who went into this very dangerous situation and handled himself so well,” said Mayor Bill de Blasio, who visited Stuart at the hospital with Bratton.

“Thank God his injuries are not life-threatening,” de Blasio said.

Police found a .380-caliber semi-automatic gun at the scene of the shooting, on Lincoln Avenue, and three more firearms and a knife at the scene of the reported fight, the area around 137th Street and Third Avenue.

Rice has five prior arrests, including one for fare-beating, Bratton said. He said Rice was freed from court on the fare-beating charge at 11 p.m. Friday, three hours before the melee.

Stuart’s father is a NYPD detective who is on active duty with the Army, Bratton said.

“A police family,” de Blasio said.

With Matthew Chayes and Cristian Salazar