TEL AVIV—Israeli Prime Minister Benjamin Netanyahu’s rivals agreed to form a coalition government that would dislodge the country’s longest-serving leader, a major political shake-up as the nation seeks to protect a fragile truce with the Palestinian militant group Hamas.

If the government is sworn in within the next two weeks, Mr. Netanyahu would cede power to the most diverse coalition in Israel’s history, including an independent Arab party for the first time. Yair Lapid, who leads the centrist Yesh Atid party, and Naftali Bennett, who heads the right-wing Yamina party, will team up with six other parties, including one of Israel’s Arab parties, Raam, according to a statement from Mr. Lapid.

“The government will do everything it can to unite every part of Israeli society,” Mr. Lapid said late Wednesday.

Mr. Lapid has informed Israel’s president of his government formation plan, the statement said.

The parliament by law has about 12 days to swear in the government after Mr. Lapid notifies the president.

Israeli Prime Minister Benjamin Netanyahu has been at the helm since 2009.

Israeli Prime Minister Benjamin Netanyahu has been at the helm since 2009.

Photo: sebastian scheiner/Agence France-Presse/Getty Images

Mr. Netanyahu can still try to undo the coalition deal in this period by convincing enough right-wing lawmakers to vote against the new government.

The deal comes days after Mr. Bennett made public his plans to join Mr. Lapid in a coalition.

Both leaders would take turns at leading the government, with Mr. Bennett serving first as prime minister for two years followed by Mr. Lapid, who would first serve as the foreign minister.

“With God’s help, we’ll do what’s good for Israel and get it back on track,” Mr. Bennett told Israel’s president in a phone call together with Mr. Lapid, according to a video of the scene published by Mr. Bennett’s party.

Blue and White’s Benny Gantz would be the defense minister, New Hope’s Gideon Saar would be the justice minister, Yisrael Beiteinu’s Avigdor Lieberman would be the finance minister and Yamina’s Ayelet Shaked would be the interior minister, officials involved in the talks said.

Outside the hotel where negotiations between the parties took place near Tel Aviv, hundreds of people gathered on opposite sides of the street to either express support or opposition to the potential new government, according to TV footage.

The parties will have to set aside their ideological differences on key issues as the coalition tries to resurrect an economy hurting from Covid-19 lockdowns while also keeping Israelis secure amid heightened tensions with Hamas. Fighting between Israel and Hamas last month killed 256 Palestinians, including 66 children, and 12 people in Israel, including two children.

Mr. Lapid received the mandate after Mr. Netanyahu failed to form a government following an inconclusive March election, the country’s fourth since 2019.

Mr. Bennett controls seven seats in the Knesset, Israel’s parliament, compared with Mr. Lapid’s 17. But right-wing Jewish Israelis who previously supported Mr. Netanyahu’s governments might find him a more politically palatable candidate as prime minister.

Mr. Bennett’s Yamina party released a video celebrating the agreement. “We’re getting out of the [election] loop [and] establishing a government. Do you support unity? Then let’s hear it from you,” the video said.

President Biden and Israeli Prime Minister Benjamin Netanyahu have been working together for decades, but their friendship faces new trials amid the Israel-Hamas truce. WSJ's Gerald F. Seib takes a look at key moments from the last 15 years of their relationship, and what might come next. Photo illustration: Todd Johnson The Wall Street Journal Interactive Edition

Messrs. Bennett and Lapid faced a difficult task in stitching together a coalition from a group of rivals, some of whom are united only in their desire to replace Mr. Netanyahu, who is on trial for corruption. He denies any wrongdoing.

Mr. Netanyahu’s case is ongoing, and analysts said he is better positioned to face the charges while remaining prime minister. Out of the office, he will be unable to advance any legislation or make any appointments that could insulate him from the charges.

Before Wednesday’s deadline, Mr. Netanyahu’s supporters, including lawmakers from his right-wing Likud party, went all out to convince some right-wing lawmakers to abandon the prospective coalition, according to people familiar with the matter.

The defection of just one or two lawmakers can prevent the new coalition from mustering a majority and force another election.

Mr. Netanyahu enjoys support among Israel’s right-wing and religious voters, which has helped him maintain a continuous grip on power since 2009.

SHARE YOUR THOUGHTS

How would a coalition government in Israel address challenges including relations with Hamas and the post-pandemic economic recovery? Join the conversation below.

He also served three years as prime minister in the 1990s. He has described the new coalition as a dangerous and left-wing government that wouldn’t be able to confront Iran or meet other security challenges.

“Do not form a left-wing government. Such a government is a danger for the security of Israel and a danger for the future of our country,” Mr. Netanyahu said.

He is expected to become the leader of the opposition, but may face leadership challenges from other members of his Likud party who have been frustrated by his repeated failures to form a government. He defeated such a challenge from a former protégé, Gideon Saar, in late 2019.

But as more lawmakers soured on the idea of working with a prime minister under indictment, Mr. Netanyahu ran out of options.

“He’s really managed to create an atmosphere in the Likud of deterrence against criticism, and so until they not only smell the blood but see it, nobody will break in Likud,” said Emmanuel Navon, a professor of political science at Tel Aviv University and a former member of Likud.

Liberal daily Haaretz on Tuesday featured an editorial with the headline, “Stop the Insanity.”

“There is a desperate need to replace a dangerous leader who is doing great damage to the country,” it said.

If they can swear in the government, Messrs. Bennett and Lapid face the challenge of keeping their fractious government together for more than a few months—or return to yet another election.

Mr. Bennett, 49 years old, is a former Israeli military commando who later co-founded an antifraud software firm and made millions of dollars when it was sold. He is also a former defense and education minister as well as a former aide to Mr. Netanyahu. While serving as defense minister, he presided over Israel’s first coronavirus lockdown, an effort that was initially viewed as a success. Israel’s caseload later skyrocketed after Mr. Netanyahu moved to reopen the economy but Mr. Bennett by then was no longer part of the government.

The parties in the emerging coalition agree on little: Some want to engage with the Palestinians, while others want to pursue annexation of the West Bank. They also disagree on Israel’s judicial system, with some opposing a more liberal role for the courts and others taking a broader view of what the court’s powers should be.

A new government will have to take steps to boost economic growth while keeping the pandemic in check.

Meanwhile, a cease-fire between Israel and Gaza ruler Hamas after 11 days of intense fighting remains fragile, as both sides try to consolidate their gains. Egypt is mediating indirect discussions between Israel and Hamas over longer term cease-fire arrangements.

The conflict was the worst since the last of three wars in 2014 and appeared to save Mr. Netanyahu’s position for a time. But after it ended in an unconditional truce, talks between his political rivals resumed.

Mr. Netanyahu’s Likud party won the most seats in the last election—30—but failed to unite Israel’s right-wing and religious parties around him or get some of them to work with an Islamist party so he could remain in control.

Write to Felicia Schwartz at felicia.schwartz@wsj.com

Corrections & Amplifications
Israeli Prime Minister Benjamin Netanyahu’s rivals agreed to form a new government to oust him. An earlier headline incorrectly said “Israel’s Netanyahu Ousted as Prime Minister as Rivals Form Coalition Government.” (Corrected on June 2.)