Two NYPD officers were shot Thursday night in a stairwell of a Bronx public housing complex by a gunman, who then committed suicide in a nearby apartment, city officials said.

The two were patrolling with a third officer at the Melrose Houses on East 156th Street when they encountered two men in the sixth-floor stairwell, said Deputy Commissioner Benjamin Tucker.

One of the men pulled out a gun and fired, hitting a female officer, 24, in the abdomen, and a male officer, 29, in the head, said Deputy Chief Edward Mullen, an NYPD spokesman.

The gunman then ran to a seventh-floor apartment, where he shot himself, police said. A .32-caliber semi-automatic handgun and a shotgun were recovered at the apartment, the NYPD said.

It was unknown whether either weapon was used in the shootings.

Emergency officials gather near the site where two police officers were shot in the Bronx on Thursday, Feb. 4, 2016. Photo Credit: AP / Julio Cortez

Both officers, two-year veterans based out of the 40th Precinct in the Bronx and assigned to the housing bureau, were rushed to Lincoln Medical Center in the Bronx and are expected to survive, city officials said.

advertisement | advertise on newsday

The second man in the stairwell when the gunfire erupted and several others were being interviewed Thursday night, police said.

At a news conference at the hospital, Mayor Bill de Blasio praised the wounded cops.

“Our brave officers were doing their jobs tonight in our public housing, on patrol keeping our residents safe, when they confronted an armed assailant,” said de Blasio, flanked by several NYPD officials and Patrick Lynch, president of the Patrolmen’s Benevolent Association.

“Thank God in both cases their conditions are good. Both officers have been alert and communicating and so we are praying for the best here,” the mayor said.

Lynch said he was relieved “we’re not getting the worst news.”

Get the Newsday Now newsletter!

The best of Newsday every day in your inbox.

He emphasized that the shooting shows the difficulty of “vertical patrols” in housing complexes. A veteran NYPD detective underscored that point last week.

During testimony in the ongoing Brooklyn manslaughter trial of rookie Officer Peter Liang — accused in the fatal 2014 shooting of a man in a darkened stairwell of the Louis H. Pink houses — Det. Nathan Garcia said the threat of an ambush in a project stairwell is a constant. Investigators said Liang’s 9-mm handgun accidentally discharged as he walked the darkened stairwell with his rookie partner.

A bullet struck unarmed Akai Gurley, 28, in the chest as he was on a landing with his girlfriend a floor below. He died later at a hospital.

When word got to De Blasio about the shooting Thursday night, he was already in the Bronx at Lehman College for his State of the City speech, less than five miles away. The mayor’s security detail quickly ushered him off the stage at the conclusion of his 69-minute address and drove him to the hospital where the officers were recovering

In the hectic first moments after the shooting, officers rushed to the scene, searching for suspects by air and ground, as housing residents wondered what had happened.

advertisement | advertise on newsday

“I was waiting for the elevator when I heard three shots,” said Katherine Martinez, 28. “Boom boom boom. I thought trouble trouble trouble.”

She said she looked outside and saw one cop with his hand on his head and blood coming down from the head. “He was bleeding a lot,” she said.

Calvin Guzman, 22, a resident of the complex, said he was at work when the shooting happened but was pleased the officers were expected to be OK.

“The police are just doing their job and keeping us safe.” Guzman said. “There’s no reason anyone should shoot at them.”

With Anthony M. DeStefano and Ivan Pereira

advertisement | advertise on newsday

CORRECTION: An earlier version of this story misidentified the official who described the sequence of events and injury to the police officers. Deputy Chief Edward Mullen, an NYPD spokesman, said one of the men pulled out a gun and fired, hitting a female officer, 24, in the abdomen, and a male officer, 29, in the head.