JERUSALEM – Israel’s rival political leaders broke the country’s unprecedented political impasse Monday when Prime Minister Benjamin Netanyahu and challenger Benny Gantz announced a deal to join forces and form an emergency unity government.

Under the terms of the agreement, Netanyahu would remain prime minister for the next 18 months with Gantz then succeeding him.

The agreement, following weeks of tense negotiations and brinkmanship, comes as Israel confronts a burgeoning coronavirus outbreak. Gantz, who had spent a year battling to unseat the prime minister, cited the outbreak for his willingness to serve with Netanyahu, whom he had repeatedly called “unfit to lead.”

The deal represents a triumph for Netanyahu, who has pushed relentlessly to extend his record run at the top of Israeli politics. Critics have bemoaned and observers marveled that Israel’s longest-serving prime minister has once again outrun the political obituaries written for him after his party and its allies failed to regain their majority in three straight national elections and he was indicted on corruption charges along the way.

“King Bibi,” as his unshakable base of supporters calls him, still faces trial on counts of bribery, fraud and breach of trust. His trial, initially scheduled for March, was put on hold when the coronavirus outbreak shut down most of Israel’s courts. Netanyahu, who unsuccessfully sought parliamentary immunity this year, is widely expected to use his office to further delay prosecution or to seek some form of official protection.

In any case, he will pursue these efforts with his once-sharpest public critic inside his government. Gantz’s acquiescence, which crushed supporters and shattered the ad hoc political party he led through three national elections, was a turnabout for the taciturn former army of chief of staff, who had vowed repeatedly never to sit in government with Netanyahu.

The terms of the agreement could change during the time it takes to enact the government. But details released in a joint statement describe a delicate power-sharing arrangement. Netanyahu will remain as prime minister for 18 months with Gantz serving as his deputy prime minister. The two would switch jobs in October 2021.

The deal includes safeguards to prevent backsliding. If either party tried to dissolve the government early, the other would automatically become prime minister, the statement said. The terms are to be made legally binding by the Knesset, Israel’s parliament.

Netanyahu’s faction will control the finance, health, public security, housing and construction, transport and education ministries. Gantz’s side will have defense, foreign, justice, media, cultural and economic portfolios. Gantz reportedly will be the defense minister.

The sides have agreed to delay a range of sticky policy disputes, such as control of the judicial appointments process and the recruitment of ultra-Orthodox Israelis into the military, by focusing on coronavirus issues for six months. Negotiating teams will try to hash out policy compromises at the end of that emergency period.

“They are kicking the political footballs down the road until they see if this government can function,” said Jason Pearlman, former adviser to Israeli President Reuven Rivlin.

Netanyahu has prevailed on the issue of Israeli annexation of Jewish settlements in the occupied West Bank, as endorsed by the Trump peace plan. The two Israeli leaders agreed this would come before the Knesset for action in July. Gantz had resisted fast action on the plan.

The two men reached their deal less than a week after the official clock ran out on Gantz’s individual chance to form a government, and they signed the agreement hours before the country began a national day of Holocaust remembrance.

“I promised the State of Israel a national emergency government that would work to save the lives and livelihoods of Israeli citizens,” Netanyahu said in a tweet.

The alliance between the former rivals cost both some of their longtime supporters. Current Defense Minister Neftali Bennett, part of Netanyahu’s right-wing coalition, said he would not join the government unless he retained the post. Gantz’s decision to join forces with the prime minister split apart the Blue and White party Gantz co-founded slightly more than a year ago.

But the agreement marks a much-awaited end to months of political stalemate and averts a dreaded fourth election. Israel has been mired in a stalemate since the end of 2018, unable to enact or fund programs and exhausting voters with unceasing political rancor. It took a global pandemic, seemingly, to finally break the carousel dynamic of close elections followed by futile negotiations.

Fittingly, the apparent end of the political limbo was torturous even after the two foes agreed, in principle, to team up.

After the inconclusive March 2 election, the sides were grappling over control of the Knesset. Gantz, with support from left-wing, nationalist and Arab parties, had the votes to topple the speaker, a Netanyahu ally, and advance legislation that could block the prime minister’s ability to stay in office while on trial.

Speaker Yuli Edelstein refused to allow a vote for his replacement, abruptly resigning after Israel’s Supreme Court ordered him to call the vote. But on March 26, Gantz shocked supporters by nominating himself as speaker and announcing that he was ready to negotiate a deal to serve with Netanyahu in the name of battling the coronavirus.

Gantz’s supporters were devastated. He had bowed to an opponent whose campaign allies had accused Gantz of being a terrorist sympathizer and suggested he had created sex tapes hacked by Iranian agents.

Some said he had been hoodwinked, accusations that have intensified after negotiations that frequently seemed to leave Gantz baffled and frustrated. Others accused him of betraying their common cause of finally breaking Netanyahu’s grip on power. Yair Lapid, one of Gantz’s Blue and White co-founders, said his former partner was “crawling” into the government.

“The coronavirus crisis doesn’t permit us to give up our values,” Lapid said. “This is rewarding criminality.”

Members of the Joint List, a coalition of Arab parties that had broken years of precedent by agreeing to support Gantz’s bid for prime minister, condemned him as well.

“He has now demonstrated that he is a clone of Netanyahu,” said Yousef Jabareen, a Joint List Knesset member.

Some critics said Gantz, who had never run for office before, was exhausted and outmaneuvered by Netanyahu, a wily political operative.

Opponents say Netanyahu has clung to power in part to shield himself against his legal trouble.

“Bibi will find loopholes in the deal and try to stay,” said Haaretz columnist Anshel Pfeffer, author of a Netanyahu biography.

Netanyahu has been the ubiquitous face of the massive government mobilization against the pandemic, and his approval level climbed as he appeared nearly nightly on television as a master of the smallest details. He insisted repeatedly that a unity government is crucial to fighting the outbreak, a message repeated by Rivlin.

It was an appeal that had resonance amid a jittery public that sees Netanyahu as a capable manager.

As the number of confirmed cases in Israel rises above 13,600, with 173 deaths, many credited Gantz, stuck in a no-win situation, with choosing stability over a still-elusive victory.

“He is not a traitor,” commentator Nahum Barnea said in the daily Yedioth Ahronoth after Gantz made clear that he would join his rival. “He is also no hero.”