With a victory, Netanyahu would capture a fourth consecutive term and fifth overall, which this summer will make him Israel's longest-ever serving leader. Re-election will give him an important boost as he braces for the likelihood of criminal charges in a series of corruption scandals.
The election outcome affirmed Israel's continued tilt to the right and further dimmed hopes of a negotiated solution to the Israeli-Palestinian conflict. In the final stretch of the campaign, Netanyahu had for the first time pledged to annex parts of the occupied West Bank in a desperate bid to rally his right-wing base. Annexation would snuff out the last flicker of hope for Palestinian statehood.
Both Netanyahu and his challenger, former military chief Benny Gantz, leader of the rival Blue and White party, had declared victory in speeches to boisterous gatherings of supporters. But as the night went on, there were growing signs that Netanyahu's Likud was pulling ahead.
"It's a night of tremendous victory," Netanyahu told supporters. "I was very moved that the nation of Israel once again entrusted me for the fifth time, and with an even greater trust." He said he had already begun talking to fellow right wing and religious parties about forming a new coalition.
"I want to make it clear, it will be a right-wing government, but I intend to be the prime minister of all Israeli citizens, right or left, Jews and non-Jews alike," he said. The 69-year-old prime minister has been the dominant force in Israeli politics for the past two decades and its face to the world. His campaign has focused heavily on his friendship with President Donald Trump and his success in cultivating new allies, such as China, India and Brazil.
But the corruption scandals created some voter fatigue. Along with two other former military chiefs on his ticket, Gantz was able to challenge Netanyahu on security issues, normally the prime minister's strong suit, while also taking aim at the prime minister's alleged ethical lapses.
Israel's attorney general has recommended charging Netanyahu with bribery, breach of trust and fraud. The telegenic Gantz, who has been vague on key policy issues, has presented himself as a clean, scandal-free alternative to Netanyahu.
By Wednesday morning, with 97 percent of the votes counted, Likud and Blue and White had won 35 seats each. But Netanyahu was in a stronger position to form a coalition government with the anticipated support of right-wing and ultra-Orthodox Jewish parties, Likud's natural allies.
Such a coalition could rest on 65 votes in the 120-member parliament. Earlier in the night, with fewer of the votes counted, Blue and White still appeared to be ahead by one seat and Gantz projected optimism. "Elections have losers and elections have winners. And we are the winners," Gantz told a victory rally shortly after midnight.
Netanyahu's message of unity was a sharp contrast from his campaign theme in which he accused Gantz of conspiring with Arab parties to topple him. Arab leaders accused Netanyahu of demonizing the country's Arab community, which is about 20 percent of the population.
His attacks on the Arab sector fueled calls for a boycott and appeared to result in relatively low turnout by Arab voters. Israel's central elections commission banned parties from bringing cameras into polling stations after Likud party activists were caught with hidden cameras in Arab towns.
The final results are subject to change. Some 40 parties took part in the election, and only those that receive at least 3.25% of the votes make it into parliament. Once the final results come in, attention will turn to President Reuven Rivlin. The president, whose responsibilities are mostly ceremonial, is charged with choosing a prime minister after consulting with party leaders and determining who has the best chance of putting together a majority coalition. That responsibility is usually given to the head of the largest party.
The election included several other surprises. The Labor party, which ruled the country for its first 30 years, tumbled to single digits in the parliament. Zehut, an iconoclastic party that combined an ultranationalist ideology with libertarian economic positions and calls for the legalization of marijuana, had appeared poised to emerge as the Cinderella story of the election. It fell short and failed to enter parliament.
If Netanyahu is re-elected, attention will quickly focus on his legal woes. The attorney general has recommended a series of criminal charges against the prime minister but will only make a decision on indicting him after a legally mandated hearing. Legal experts expect at least some charges to be filed.
Netanyahu will likely focus his efforts on getting guarantees from his coalition partners to continue to back him if he is indicted, and perhaps find a way to grant him immunity from prosecution. Netanyahu has denied any wrongdoing and claimed he is the victim of a witch hunt.
In order to appeal to his religious and ultranationalist parties, Netanyahu veered sharply right on the campaign trail with attacks on Arab politicians, the media and the judiciary. He also pledged to annex West Bank settlements, a step that could snuff out any remaining hopes for a two-state solution with the Palestinians.
Palestinian official Saeb Erekat lamented that the Israelis voted to maintain "the status quo." "They want their occupation to be endless," he said.
Heller reported from Tel Aviv. AP journalists Ilan Ben Zion and Isabel DeBre contributed to this story.
Follow Heller at www.twitter.com/aronhellerap