Two New York City police officers were shot and injured while patrolling a Bronx housing project by a suspect who later took his own life late Thursday.

Authorities said both injured officers were in stable condition. One officer's nose was grazed by a bullet, while the other was shot in the hip. 

One of the officers is a 29-year-old man, and the other is a 24-year-old woman, said Robert Boyce, the police department's chief of detectives. The officers, who are assigned to the Housing Bureau, have been on the force for about two years. Police would not provide the names of the officers or the suspect.

Investigators said the officers were on the sixth floor of an apartment building in the Melrose Houses complex when they encountered two people in a stairwell. One of the people pulled a gun and opened fire.

The gunman fled into an apartment on the seventh floor, New York Police Department First Deputy Commissioner Ben Tucker said. Officers responding to the scene found the gunman in the apartment dead of an apparently self-inflicted gunshot wound. A handgun and a shotgun were found inside the apartment.

The second person who encountered the officers in the stairwell was in custody, and three people in the apartment were being questioned, Tucker said.

The shooting occurred approximately five miles from where New York City Mayor Bill de Blasio was delivering his State of the City address. De Blasio was was told about the shooting as he finished his speech and left the stage. He met with the family of one of the officers at the hospital where they were being treated.

"Our brave officers were doing their jobs tonight in our public housing on patrol keeping residents safe," de Blasio said. "Both officers are alert and communicating, and we are praying for the best here."

The head of the police officers' union, Patrick Lynch, said the shooting shows the dangerous nature of the job.

"We need your support to teach our young folks that pulling a gun on a police officer works for no one," he said. "This goes to show the dangers police officers face each and every day."

He said the shooting shows the difficulty and danger of vertical patrols, on which pairs of officers start in the lobby of a public housing project and walk the stairwells up to the roof and back down.

In January, a police officer responding to a large street fight in the Bronx was shot in an ankle. And in October, a police officer responding to a report of shots fired and a bicycle stolen at gunpoint in Manhattan's East Harlem neighborhood was killed.

Click for more from Fox5NY.com.

The Associated Press contributed to this report.