It was divine intervention.

The gun used by a hate-filled shooter to blast through a San Diego synagogue Saturday miraculously jammed after only a few shots, the house of worship’s Brooklyn-born rabbi told The Post on Sunday.

“One of the miracles was that his gun jammed after the first six rounds,” Chabad of Poway Synagogue Rabbi Yisroel Goldstein, 57, said by phone from his hospital bed, where he’s recovering from a gunshot wound to the hand.

Suspect John T. Earnest, 19, is accused of killing one congregant and injuring the rabbi and two others before the malfunction.

Goldstein recalled the man storming into the synagogue.

“I turned around instinctively and locked eyes with the shooter, who then aimed his rifle right at me,” he said. “I instinctively put my hands up to protect myself, and I was trying to save [congregant] Lori [Gilbert-Kaye]. But then he got his shots off on my fingers. He took off my right index finger and severely injured my left finger.”

Gilbert-Kaye, 60, is being hailed as a hero for diving in front of the rabbi to shield him from the bullets.

When the suspect’s gun jammed, two congregants — an off-duty border-patrol agent and an ex-Marine — made a B-line for Earnest, and he “took off,” the rabbi said.

With the shooter on the run, the rabbi turned his attention to his flock.

“There was a group of children playing in the banquet hall, so I gathered them all together and brought them outside. Meanwhile, blood was spurting from my wounds. I wrapped them with my [prayer shawl], and then I ran back to the shul to get everyone out of shul right away,” said Goldstein, a father of six and grandfather of 10.

“You wouldn’t want to imagine this. It was your worst nightmare, like being in Nazi Germany all over again.”

He said he underwent four hours of surgery on his hands.

President Trump called Goldstein on Sunday.

“He spent close to 10, 15 minutes with me on the phone,” the rabbi said. “He spoke about his love of peace and Judaism and Israel.”

Goldstein was raised in Crown Heights, Brooklyn, and moved to California at age 22 to start his congregation.

“Growing up in Brooklyn, New York, was not a very safe place,” he mused. “I came to Poway because I knew it was a very safe place.”

Earnest’s first hearing is set for Wednesday.

Additional reporting by Lee Brown