CNN  — 

A coalition of Israeli political parties announced Wednesday night they had agreed ​to a deal to form a new government, paving the way for the exit of Israel’s longest serving prime minister, Benjamin Netanyahu.

Yair Lapid, leader of the centrist Yesh Atid party, notified Israeli President Reuven Rivlin that he had managed to cobble together a coalition just 38 minutes before the midnight deadline.

Though Lapid holds the mandate to form the government under the agreement, it is Naftali Bennett – leader of the small right-wing party Yamina and the kingmaker in coalition talks – who is set to become prime minister for the first two years of a four-year term. Lapid will serve as foreign minister until the two men swap roles halfway through the term.

In a statement, Lapid said the “government will work to serve all the citizens of Israel including those who aren’t members of it, will respect those who oppose it, and do everything in its power to unite all parts of Israeli society.”

The deal is a seismic event in recent Israeli political history as it could spell the final days of Netanyahu’s 12 years in office.

The coalition agreement must now pass a vote of confidence in the Knesset, Israel’s parliament, before a new government and prime minister are sworn in.

The new government ​consists of a number of parties from across the political spectrum, from the left-wing Meretz to Bennett’s right-wing Yamina party.

In a historic moment, the small Islamist party United Arab List also decided to join the coalition, the first time an Arab-Israeli party has joined a coalition. The party is unlikely to have a minister in the government, but will have negotiated with the coalition on issues important to them.

Not much unites the parties other than their desire to oust Netanyahu, and the parties differ in their stances on some of the country’s most pressing issues, especially relations with the Palestinians.

Bennett is the son of American immigrants and a former elite commando in the Israeli military who made millions in the Israeli tech industry.

He previously served as defense, economy and education minister under Netanyahu, but is politically even more right-wing than the longtime Israeli leader on certain issues, especially pertaining to relations with Palestinians. But he’s more moderate when it comes to social issues such as LGBTQ rights.

Bennett once led a West Bank settler organization and has previously said he did not believe in a two-state solution that would establish a state for Palestinians in the West Bank and Gaza.

Israelis have gone to the polls four times over the past two years, as election after election failed to break the political deadlock. Bennett said in a speech on Sunday that he was willing to sit in a coalition with parties on the opposite end of the political spectrum in order to prevent a fifth round of elections and to pull Israel out of its “tailspin.”

After the March elections, the Israeli President had tasked Netanyahu to form a government after the vote with​in 28 days, per election law. But the Prime Minister was unable to command enough support to control a ​minimum of 61 seats in Israel’s 120-seat parliament, the Knesset.

The President then tasked Lapid with forming a government. The talks appeared to fall apart two days into the latest conflict with Hamas-led militants in Gaza last month when Bennett announced he was suspending negotiations due to the violence. But a few days after the ceasefire was reached following 11 bloody days, Bennett and Lapid reignited negotiations.

According to Israeli law, the Knesset must hold a vote of confidence within a week of being formally notified of a new government being formed. This step might not happen until Monday, which means the vote could be held as late as June 14.

That means there’s still time for Netanyahu and his allies to convince people to defect from the bloc, or somehow tie things up procedurally in parliament. A collapse of the ceasefire with the Hamas-led militants in Gaza or another outside event could also topple the burgeoning new government.

In the meantime, Netanyahu remains prime minister until someone else is sworn in. Beyond losing his role as leader of Israel’s government, Netanyahu faces perhaps an even greater threat: an ongoing trial on charges of bribery, fraud and breach of trust – charges he denies.

If Netanyahu is ousted, it could put to an end any political attempts to circumvent the trial and possible jail time by appointing a new attorney general, or by influencing the appointment of certain judges who could affect his trial, experts said.

Netanyahu blasted Bennett on Sunday for committing what he called the “fraud of the century,” portraying him as a power-hungry politician who “only cares about himself.” It was a statement that some Israeli political watchers found to be more than ironic, considering Netanyahu’s past political maneuvers.