Surveillance video obtained by The Post shows the moment a masked gunman opened fire at NYPD officers on a Brooklyn street early Monday morning, touching off a wild shootout with cops that ended in the shooter’s death.

The gunman is seen sprinting from the officers as they tail him in a police cruiser on Howard Avenue near Dumont Avenue in Brownsville at about 2:30 a.m., according to the footage.

The suspect then whips out a handgun from his back waistband and squeezes off at least one round at the cops.

One of the officers in the car gets out and returns fire, shooting back at the suspect at least four times.

The masked gunman — identified by sources as Brandon Clayton — evaded cops after the initial shootout, but was tracked down about half an hour later to a backyard on Howard Avenue, NYPD Chief of Department Terence Monahan said at a press conference after the shooting.

As police responded to the backyard, the suspect set off a barrage of gunfire in their direction, police said.

“Our officers returned fire and the man was then taken into custody,” Monahan said at the press conference.

The gunman was taken to Brookdale Hospital, where he was declared dead. A semiautomatic handgun was recovered at the scene.

Monahan said investigators were still combing through the crime scene, but he estimated police fired more than 50 shots during the wild shootout.

He lauded the cops on their bravery.

“The great work by our cops today demonstrates, once again, how they never hesitate to go into harm’s way to keep our city safe,” the chief said.