By David M. Halbfinger and Patrick Kingsley, New York Times–

Prime Minister Benjamin Netanyahu of Israel held a significant lead in his third electoral showdown with the former army chief Benny Gantz, but fell short of an outright parliamentary majority, after more than 90 percent of votes had been counted on Tuesday morning.

The tallies showed Mr. Netanyahu, who faces trial in two weeks on felony corruption charges, coming within two seats of winning a record fifth term in office and breaking the political logjam that has paralyzed Israel for more than a year.

Vote shares fluctuated dramatically all morning, and some ballots — from soldiers and diplomats — may not be counted for several days. But at 10:30 a.m. television networks projected Mr. Netanyahu’s right-wing and religious coalition would win 59 seats in the Monday election, two shy of a majority in the 120-member Parliament.

That plurality, if it holds up, would give him the first chance to assemble a majority coalition, but he would have to peel off at least two seats from other parties to do so.

It would also set the stage for a possible constitutional showdown between Israel’s political and judicial power centers over whether he is legally able to form a government while under indictment.

“This is a night of tremendous victory,” an ebullient Mr. Netanyahu told supporters in Tel Aviv early Tuesday. He called it “sweeter” than his first election as prime minister, in 1996, “because this is a victory against all odds.”

“They eulogized us, they said it’s the end of the Netanyahu era,” he added, “but we turned everything upside down.”

Mr. Netanyahu and his allies won 60 seats in the election last April, and he was unable to form a government then. But two elections later, Mr. Netanyahu is full of momentum, his opponents are exhausted, and analysts began speculating about possible recruitment targets for him among Mr. Gantz’s allies to get to the crucial 61.

Actual results were dribbling in during the morning on Tuesday.

Mr. Gantz and his allies on the center-left, including the predominantly Arab Joint List, appeared to have won only 53 or 54 seats, according to results released midmorning.

Speaking before most votes had been counted, Mr. Gantz had vowed to fight on, urging supporters to wait for official results and reminding them that Mr. Netanyahu would soon be “sitting in the defendant’s chair.”

But he acknowledged the sting of exit polls released shortly after voting concluded. “I realize and share your feelings of disappointment and pain,” Mr. Gantz said on Tuesday. “This isn’t the result that we wanted to happen.”

The Joint List appeared to have won at least 15 seats, which would be a record for Arab representation in Parliament. It previously held 13.

Since Mr. Netanyahu is facing felony prosecution, it is unclear whether the president, Reuven Rivlin, can legally invite him to form a government. The situation has no precedent and the Supreme Court, perhaps hoping a third election would render the question moot, avoided ruling on the subject in January.

But the court would almost certainly be asked to intervene, forcing its unelected judges to choose between disqualifying an elected leader from taking power or allowing a leader who is accused of bribery, fraud and breach of trust to form a new government.

At a minimum, Mr. Netanyahu, Israel’s longest-serving premier, could use the electoral result to bolster his hand in negotiating a plea bargain.

“This would be a very dramatic test for Israeli democracy and the rule of law,” said Gideon Rahat, a professor of political science at the Hebrew University of Jerusalem. “He’ll use his majority to say: The people supported me, and who is the court to decide otherwise?”

A new term for Mr. Netanyahu would also clear away domestic political impediments to annexing territory in the occupied West Bank, a move endorsed by the Trump administration and considered illegal by most of the world.

And it would renew the control of Mr. Netanyahu’s ultra-Orthodox allies over matters of religion and state, affecting emotionally charged matters like marriage and conversion that have torn at the fabric of Israeli society and driven a wedge between the country and liberal American Jews.

Mr. Gantz, who had tried to rally Israelis behind the rule of law, had argued that even a narrow advantage for Mr. Netanyahu in Parliament could lead to a dangerous slide into autocracy.

Mr. Gantz had also endorsed the West Bank annexation in principle, but with the important caveat that he would not proceed with it outside of a negotiated settlement with the Palestinians or without broad international support.

The prospect of an invigorated right-wing government pushing to annex West Bank land sent shudders through the Israeli peace camp and drew plaintive reactions from the Palestinians.

“It is obvious that settlement, occupation and apartheid have won the Israeli elections,” Saeb Erekat, the veteran Palestinian negotiator, wrote on Twitter, adding that Mr. Netanyahu’s campaign was on “the wrong side of history.”

The results reflect an uptick in turnout, the strongest since 2015, despite the ugliness of the campaign’s final weeks and fears of the new coronavirus.

But voting appeared markedly heavier in the strongholds of Mr. Netanyahu’s Likud party, analysts said, particularly in Israel’s so-called peripheral areas, from the desert city of Beersheba in the south to Kiryat Shemona on the Lebanese border.

By contrast, late in the day Mr. Gantz and his allies were pleading with residents of Tel Aviv, a liberal bastion, to hurry to the polls.

The left-wing Labor-Gesher-Meretz alliance appeared to collapse, to six or seven seats in Parliament, down from 11, exit polls showed. Some of its voters appeared to have gravitated toward the Joint List, which made a strong push for liberal Jewish support.

Avigdor Liberman’s ultranationalist Yisrael Beiteinu party, which abandoned Mr. Netanyahu’s bloc after the April election, was projected to win six or seven seats. That could still be enough to offset Mr. Netanyahu’s lead if he were to throw his support behind Mr. Gantz.

Special polling places were set up in outdoor tents for people quarantined because of the new coronavirus, and lines were so long that election officials added two more such locations late in the day. Several thousand people in quarantine voted, but when election officials balked at counting their ballots, emergency medical technicians volunteered.

Mr. Gantz, who first entered politics a year ago after a distinguished military career, made integrity and basic decency the core of his campaign against Mr. Netanyahu. He accused Mr. Netanyahu of Mafioso-like tactics and of wanting to hold onto power at any cost.

But Mr. Netanyahu, 70, tried to deflect attention from his legal troubles and erode Mr. Gantz’s image with swing voters. Mr. Netanyahu questioned Mr. Gantz’s competence and mental stability, while his allies orchestrated damaging leaks about Mr. Gantz’s character and announced a new investigation of his brief private-sector career. Mr. Netanyahu’s son Yair spread unsubstantiated rumors about Mr. Gantz’s personal life.

In one sordid chain of events, Mr. Gantz’s top strategist unburdened himself to a rabbi and, in the process, denigrated his client. The rabbi turned out to have been secretly recording their conversation. The tape wound up on the nightly news last week.

The prime minister denied any hand in it, but yet another recording showed Mr. Netanyahu had been directly involved.

The mudslinging and cutthroat machinations prompted Mr. Rivlin to offer an apology to the Israeli public on Monday morning.

“I have a bad sense, even a sense of shame when facing you,” he said upon casting his ballot in Jerusalem. “We simply don’t deserve this,” he added. “We don’t deserve another terrible election campaign that declines to the point of filth.”

Mr. Rivlin will give the mandate to form a new government to the candidate he believes has the best chance of assembling a majority. If that is Mr. Netanyahu, his effort could be delayed by a court challenge. Even if Mr. Netanyahu proceeds, he may not be able to find the majority that has eluded him twice before.

If Mr. Netanyahu needs to recruit another party to his coalition, a new round of political poker could ensue, in which party leaders will come under enormous pressure to make compromises in order to spare the country a fourth election.

The process could leave Israel stuck in political deadlock for weeks or months to come.

In public, Mr. Netanyahu was a whirlwind of upbeat energy on the stump, promising new hospitals, lower prices and other inducements tailored to marijuana smokers, Ethiopian-Israelis and Arab citizens, among others.

By contrast, Mr. Gantz’s message, that Mr. Netanyahu had lost the moral authority to lead, was dour and one-note.

Some voters said they had been turned off by the ugliness of Mr. Netanyahu’s campaign, and by the criminal charges against him.

“Bibi is corrupt and his campaign was dirty,” said Rotem Cnaan, a 33-year-old shopkeeper in Rehovot who said he usually votes for Likud but sat out Monday’s vote. “I can’t hear his voice anymore, I can’t stand his intonation, and his son Yair is just shocking.”

But Netanyahu loyalists said they saw him as a statesman of extraordinary gifts. “I’m a big believer in Netanyahu,” said Kobi Dadon, a leader in a firefighters’ union, after voting in Jerusalem. “We have never had such a leader in Israel. We had Begin and Rabin, and he is that caliber.”

The acrimonious campaign was the capstone to an unusually bitter year in Israeli politics. Mr. Netanyahu’s caretaker government lacked the mandate to make major spending and policy decisions, while politicians of all stripes were distracted by the back-to-back-to-back election fights.

Though Mr. Netanyahu last year passed the record of David Ben Gurion, Israel’s first prime minister, to become the country’s longest-serving head of government, he also became the only sitting prime minister to be indicted on criminal charges. He was charged in November with offering political favors to media moguls in exchange for fawning coverage and expensive gifts.

Much was at stake for Israel’s relations with the Palestinians, particularly in light of the Trump administration’s green light to Israel to begin annexing West Bank territory, and Mr. Netanyahu’s promise to do so after the election.

Arab citizens of Israel, who make up about 20 percent of the population, showed unusual energy heading into Monday’s vote, believing they at last had the opportunity to help topple Mr. Netanyahu, who has long tapped into anti-Arab sentiment to whip up support from his right-wing Jewish base.

With a well-financed and spirited get-out-the-vote effort, the combined slate of predominantly Arab parties known as the Joint List hoped for as many as 15 seats. It now holds 13, a high-water mark. A religious Muslim woman in the list’s No. 15 spot, Iman Khatib Yasin, stood a chance of becoming the first hijab-wearing lawmaker in Israeli history.

Many Arab voters were also enraged by Mr. Trump’s plan — widely referred to as the “Deal of the Century” — because it would legitimize Israeli settlements in the occupied West Bank, give Israel complete control of Jerusalem, which the Palestinians want as their capital, and severely restrict the sovereignty of a proposed Palestinian state.

“I never believed that the Parliament elections could be beneficial for the Arabs,” said Yazeed Baloum, a 33-year-old building contractor, after voting in the Arab town of Taybeh. “But Netanyahu’s racism and incitement against us made me determined to vote — as well as the Deal of the Century.”

Mohammed Najib contributed reporting from Taybeh, Israel, and Gabby Sobelman from Rehovot, Israel.

Mohammed Najib contributed reporting from Taybeh, Israel, and Gabby Sobelman from Rehovot, Israel.