Israel’s president is set to begin two days of consultations with political parties after a deadlocked election last week plunged the country into uncertainty over who will lead the next government.

Near-final results from Tuesday’s poll showed the opposition chief, Benny Gantz, marginally ahead of the prime minister, Benjamin Netanyahu, with his Blue and White party taking 33 seats out of parliament’s 120. The ruling Likud party has 31.

Critically, neither side appeared able to forge a majority government, even with support from allies in smaller parties.

On Sunday afternoon President Reuven Rivlin will meet both leaders in an attempt to break the stalemate or face the possibility of a potential third round of elections in less than a year. Rivlin holds a largely ceremonial post but is also responsible for choosing the candidate he believes has the best chance of forming a government. Usually, the decision is clear, and often goes to the leader of the largest party, but the muddied result has created an impasse.

Despite being Israel’s longest-serving leader and having a reputation for political sorcery, Netanyahu is fighting a tough battle. On Thursday he acknowledged his plan had failed. “During the elections, I called for the establishment of a rightwing government,” Netanyahu said in a video message. “But unfortunately the election results show that this is not possible.”

After leading the country for 10 consecutive years, Israeli media has questioned whether his era was over. His biographer, Anshel Pfeffer, wrote that, while he may still cling on, “the Netanyahu magic has been broken”.

Fearing defeat, the prime minister has called for his opponent to join him in a unity government, hinting that he might be willing to accept a power-sharing arrangement with Gantz. There is a precedent in Israel for political rivals to serve together after Yitzhak Shamir and Shimon Peres rotated the role of prime minister in the mid-1980s. However, Gantz, a former military chief, swiftly rejected Netanyahu’s offer and said he should lead because his alliance won the most seats. “We will not be dictated to,” he warned.

Israel has held two elections in five months after Netanyahu failed to cobble together a coalition following a similar result in April. Rather than give the opposition a chance to do so, he instead pushed to dissolve the Knesset, triggering repeat elections and giving himself another opportunity.

The gamble has left him in an apparently worse position and the stakes are much higher. In two weeks’ time he will face pre-trial hearings for three corruption cases against him. A majority in the Knesset could give Netanyahu – who denies any wrongdoing – immunity from prosecution.

Avigdor Lieberman, whose party took eight seats, could emerge as kingmaker. Photograph: Jalaa Marey/AFP/Getty Images

At the centre of the impasse, and the man with the key to ending it, is Israel’s apparent kingmaker – the far-right ultra-nationalist Avigdor Lieberman. The staunch secularist took eight seats, but his refusal to join a government with Jewish religious groups has added further blocks.

Politicians from an alliance of the country’s minority Arab population could also play a role, after they became the third-largest bloc in the Knesset. Ayman Odeh, the head of the group, has said that he may back Gantz, but even that would not give the opposition figure a majority.

If Sunday’s talks prove fruitless, Rivlin’s office said he might invite Netanyahu and Gantz back for more consultations. The president is obliged by law to choose a candidate by 2 October, who will then have up to six weeks to form a government. If that person fails, the president can task another, but the process could break down and force the holding of a third election.

Rivlin has said he will do everything in his power to avoid such a costly scenario that would paralyse Israeli politics right into 2020. Yet some say it looks increasingly likely.

“These are early days indeed to try to make sense of what government may emerge from the migraine-inducing complexity of Israel’s elections,” wrote David Horovitz, founding editor of the Times of Israel. “But the outcome everybody professes to want to avoid is already starting to loom in the distance.

“If Netanyahu sees it as his last hope, and Gantz thinks he’ll emerge from it stronger, we may yet have to go through this all again.”