TEL AVIV—Both Prime Minister Benjamin Netanyahu and his challenger, former Gen. Benny Gantz, declared victory in Israel’s election after a hard-fought race that underlined Israel’s rightward drift and tested voters’ tolerance for a leader facing corruption allegations.

Most exit polls showed Mr. Netanyahu’s center-right bloc of parties winning a majority, but most surveys had Mr. Gantz’s party winning the most seats in parliament. Both men said they would try to form a government in the coming days. Final election results were expected Wednesday morning.​

The election was widely seen as a referendum on Mr. Netanyahu and his decades in public life, including 13 years as prime minister over four terms. He ran as an indispensable statesman, getting boosts from his close ally President Trump, who received him at the White House, recognized Israel’s sovereignty over the Golan Heights and dispatched Secretary of State Mike Pompeo to Israel less than a month before the vote.

Mr. Netanyahu jolted the election with a last-minute proposal to extend Israeli sovereignty to West Bank settlements, underscoring how some right-wing ideas once considered implausible have become mainstream. The promise frayed relations with the Palestinians further just as the Trump administration prepares to unveils a peace plan.

As Mr. Gantz harped on his rival’s charges for bribery, fraud and breach of trust, Mr. Netanyahu sought to deflect them, denying the allegations and casting the probes as a witch hunt. If he remains leader, Mr. Netanyahu faces the possibility of becoming the country’s first sitting premier to be criminally charged.