When it comes to elections, Israel’s Benjamin Netanyahu is the political equivalent of a scrambling quarterback. During the three-month election campaign that ended Tuesday night, the prime minister fell 20 yards behind the line of scrimmage before evading a savage blitz, escaped a catastrophic sack, then, at the last moment, turned upfield, crossed the line of scrimmage and galloped ahead for a huge gain.

Although Netanyahu’s Likud Party only won about half the number of seats needed to secure a majority in Israel’s parliament, it did so much better than anyone (including Bibi) expected that he seems to have triumphed in the goal he had set for himself last December when he broke up the government he had formed in 2013 and called new elections.

He did that because he wanted to strengthen his own hand and rid himself of two hostile coalition partners at the same time. This was a risky strategy, and it looked for quite a while like a disastrous one.

Throughout the final weeks, polling suggested Likud had fallen behind the center-left opposition, the Zionist Union.

If Likud had come in second Tuesday night, the ZU might have been given the first crack at forming a new government — and though it would have had profound difficulties assembling that new government, it’s possible the ZU might have succeeded.

But had it failed, Bibi would then have been left with the horrid task of slapping together some kind of nightmarish contraption coalition to secure the 61 seats necessary for a majority. He would have been weak and his government would have been on the verge of collapse from the moment it was assembled.

Something else — something entirely unexpected — happened. Bibi’s Likud was expected to secure 20 to 22 seats in the Knesset on Tuesday night. That’s what all the polls showed. Instead, it appears Likud won 29, maybe 30 seats.

Not only was that shockingly good for Likud, it was a far stronger showing than in the last election, because in 2013, the party had merged with another called Yisrael Beytenu.

In 2013, Likud and Yisrael Beytenu together won about 23 percent of the vote. Tuesday night, Likud alone won 24 percent (or so the results seemed to suggest at press time).

Take it still further: Yisrael Beytenu evidently won six seats last night. So, if you unofficially reassemble the 2013 merger and unite its total with Likud’s, their overall number is 36 to 37 seats — a nearly 15 percent gain in two years.

Bibi’s path to forming a new government is clear and should be relatively easy, especially compared to last time.

He gambled his entire career and he won, just as he did with the speech in front of Congress earlier this month.

The three-month election process was heart-stopping and melodramatic, like an old “Batman” episode from the 1960s without the camp — including the startling role played by Special Guest Villain Barack Obama doing everything in his power to take down the man he seems to have chosen as his Enemy No. 1.

The president (or his team) shipped close campaign aides to Israel to help Bibi’s opponents, and one State Department-funded group helped coordinate the line of attack.

The strategists going after him figured out that the key to the election was to stimulate what might be called “Bibi exhaustion” in the electorate. Their approach was to remind centrist voters of Bibi’s failure to do anything about the nation’s spiraling cost-of-living crisis, which had helped bring hundreds of thousands of Israelis into the streets in mass protests in 2011.

It worked. A bit. The Zionist Union won something like 24 seats, an improvement for its core Labor Party from 15 seats just two years ago. But the thing is, Israel’s left doesn’t have any answers to the nation’s pressing problems either — and is seen as too ready to capitulate on hard-core defense and security issues.

So, in the final days of the election, trapped in his own backfield, the Zionist Union coming hard upon him as Obama and his minions cheered from the skyboxes, Bibi eluded its grasp and made his move. Love him or hate him, you have to admit that Bibi pulled off a pretty spectacular piece of footwork in the most exciting election finish I can remember.