The gunman was arrested at the police station. The two officers were being treated in hospital and were expected to fully recover from their wounds, officials said at a news conference.
"This was an attempt to assassinate two police officers ... it was a premeditated effort to kill," New York City Mayor Bill de Blasio said. "An attack on a police officer is an attack on all of us."
The gunman, whose name has not yet been released by authorities, entered the 41st Precinct headquarters in the Bronx borough just before 8 a.m. (1300 GMT), pulled out a .9mm hand gun and started firing at the front desk where several officers stood, officials said.
He then walked into an area next to the desk and fired several rounds at point blank range at several more officers and a civilian staffer, Police Commissioner Dermot Shea said.
"It is only by the grace of God and heroic actions of those inside the building who took him into custody that we are not talking about police officers murdered," he said.
"This coward immediately laid down but only after he ran out of bullets," Shea said.
A lieutenant, who returned fire, was shot in the upper left arm.
On Saturday night a uniformed police officer who was sitting in his police van with his partner was shot by the same gunman, police said. He walked up to the vehicle and began a conversation with the two officers in the vehicle before suddenly opening fire, striking one policeman in the chin and neck, police said.The officers did not return fire. The wounded officer's partner got in the driver's seat and drove him to the hospital.