Two NYPD officers were shot while on routine patrol inside a public housing complex in the Bronx Tuesday night, but both are expected to be survive, officials say.

The cops were shot inside the Melrose Housing complex in the South Bronx at about 8 p.m., officials said at a news briefing Thursday night. Three officers assigned to Police Service Area 7 -- the unit that patrols public housing within the NYPD's 40th and 42nd precincts -- were conducting vertical patrol inside a building when they encountered two people in the sixth-floor stairwell.

One of the men allegedly shot at the officers, wounding two of them -- one in the face and one in the abdomen, officials said. The gunman fled into a seventh-floor apartment where an acquaintance lived.

He ran to the back of the apartment, allegedly pulled out a gun and screamed, "I just shot a cop, I'm not going back to jail," then shot himself, sources told NBC 4 New York.

Responding officers got into the apartment and allegedly found the suspect dead of an apparent self-inflicted gunshot wound, officials said at the briefing.

Neighbors said police swarmed the area immediately.

"The cops just started running over, 'cause the police station is right down the block, so all the cops came running down here right away," said one neighbor.

"Everything was quiet and then all of a sudden boom, boom, boom, all over the place, and I came outside to see what was going on," another resident, Giselle Cruz, said.

The other person from the stairwell turned himself in to police, but police do not believe he is connected to the shootings, sources said. Police are speaking to the other people who were inside the apartment at the time.

Officers recovered a shotgun and a semiautomatic weapon at the scene. Police were in the process of obtaining a search warrant to recover more evidence from the apartment.

The injured officers were taken to Lincoln Hospital, where they were said to be in stable condition. Sources had initially said one of the officers was critically wounded.

Mayor Bill de Blasio said the officers were alert and communicating.

"Our hearts and our prayers are with their families," he said at the briefing. "Thank God in both cases their condition is good."

The officers have not been identified. Officials would only say one was a man and one was a woman, and each had two years on the job.

The female officer who was shot in the abdomen was undergoing surgery, sources said.

The mayor was in the Bronx at the time of the shooting, giving his state of the city address about 5 miles away at Lehman College. It did not appear he was aware of the shootings as he spoke.

He wrapped up his speech shortly after 8:30 p.m. His spokeswoman said he was briefed as soon as he left the stage and then headed to the hospital.

-- Andrew Siff, Marc Santia and Melissa Russo contributed reporting