The masked gunman who shot a political power broker at his opulent Long Island castle attempted to squeeze off more rounds, but his pistol apparently misfired, surveillance video shows.

“After he shot, it looked like the gun malfunctioned,” a law-enforcement source told The Post Wednesday.

The firearm foul-up may have saved the life of Gary Melius, who was shot once in the face while sitting in his Mercedes-Benz outside the Oheka Castle in Huntington on Monday afternoon.

It’s unclear how many shots were fired, but surveillance video shows that the shooter and an accomplice who served as the getaway driver used an unsecured service entrance to get in and out of the sprawling estate, the source said.

The main gate to the historic mansion-turned-hotel and event space is protected by a security guard and a gate that’s kept closed until visitors are identified and granted access.

The source said surveillance video also shows Melius, 69, climbing out of the car after he was hit and stumbling back toward the building, where he lives with his wife, Pam.

A daughter, Kelly Melius, who’s director of sales at Oheka, found her wounded pop and rushed him to the hospital.

The bullet that hit Melius above his right eyebrow didn’t penetrate his skull, and he’s recuperating at North Shore University Hospital.

His visitors on Wednesday included Suffolk County DA Thomas Spota, who spent more than an hour by Melius’ bedside.

Spota declined to comment as he left the hospital.

A Suffolk County police spokeswoman also said cops wouldn’t answer any questions about the search for suspects or the motive behind the attack.