A masked gunman was killed in a wild shootout with NYPD officers early Monday that began on a Brooklyn street and ended in a resident’s backyard with dozens of rounds fired, authorities said.

Three uniformed police officers patrolling the Brownsville neighborhood in an unmarked car spotted the man walking on Howard and Dumont avenues around 2:30 a.m., Chief of Department Terence Monahan said at a news conference.

RETIRED NYPD SERGEANT REPORTEDLY KILLS SELF AMID DEPARTMENT'S 'MENTAL-HEALTH CRISIS'

"He had his face covered walking down the street, which is kind of suspicious on a warm evening, so they went to stop him. He immediately ran," Monahan said.

When one officer got out of the car and the two others turned the corner on Howard Avenue, the man fired on the police vehicle, leaving a bullet hole in the front passenger-side door, the chief said. The officer on the street fired back, but the man ran away.

About 30 minutes later, a Howard Avenue resident called police about a man trying to break into his home from the backyard. Uniformed officers entered the yard, but were pinned down when the man opened fire, Monahan said. The gunfire continued as backup arrived.

Officers returned fire and the man was eventually taken into custody.

The unidentified man, who was hit “numerous times” in the body, was taken to Brookdale Hospital, where he was pronounced dead, the chief said. No officers were injured.

CLICK HERE FOR THE FOX NEWS APP

One officer fired about nine rounds during the first encounter on Howard Avenue, while six others fired about 55 rounds in the backyard, Monahan told reporters. Police officers were still counting the shells the gunman fired.

Police said officers recovered a semi-automatic handgun from the scene and have video of the encounter.