In a dramatic shift that took the entire Israeli political establishment by surprise, Prime Minister Benjamin Netanyahu and Kadima Chairman Shaul Mofaz formed a unity government and canceled early elections in the small hours of Tuesday morning.

Shortly after 2:30 a.m., Netanyahu and Mofaz arrived at the Knesset to brief their parties of the details of their agreement. Kadima joined the government in exchange for Mofaz’s appointment as a deputy prime minister, a minister without portfolio, and a cabinet member.

Additionally, the government will propose a replacement for the Tal Law on national service for the ultra-Orthodox, which is set to expire in August. There will be legislation on electoral reform, and the budget will be passed smoothly, the two agreed. Mofaz said that in the coming year Kadima will receive additional ministerial positions. Both parties have agreed that the 18th Knesset will complete its term and elections will be held on schedule in late 2013.

Mofaz hailed the agreement as “as unprecedented deal” which will permit electoral improvements and equal division of national service burdens.

The meeting between Netanyahu and Mofaz ran throughout Knesset deliberations to dissolve the government on Monday evening. Defense Minister Ehud Barak played a role in orchestrating the Netanyahu-Mofaz agreement, Ynet News reported.

The Likud and Kadima factions approved the partnership. Coalition partners Eli Yishai (Shas) and Avigdor Liberman (Yisrael Beytenu) also expressed support for the deal.

President Shimon Peres lauded the formation of a national unity government as “good for the people of Israel and welfare of the state.”

The sensational move was disclosed shortly after the government’s bill to dissolve the Knesset had passed its first reading Monday night, by 109 votes to 1, with Kadima supporting it. Parliament was then proceeding toward the second and third readings of the bill, en route to general elections on September 4.

“Moments before the dissolution of the Knesset, a hasty meeting to establish a national unity government,” Likud MK Carmel Shama Cohen wrote on his Facebook wall.

Initial reports indicated that Netanyahu and Mofaz identified a common interest in staving off early elections and forging a new unity partnership: It would reduce the prime minister’s dependence on the smaller factions that have been pressuring him, and it would give Mofaz a chance to try to build up Kadima’s public standing. In recent polls, it has been heading for only about a dozen seats if elections are held in the near future.

Netanyahu’s Likud, with 27 seats, and Kadima, with 28, are the two largest parties in the current Knesset. Netanyahu and Kadima’s previous leader, Tzipi Livni, discussed a unity government after the 2009 elections, but could not agree on terms, and did not get on well personally. The Netanyahu-Mofaz relationship, though not without friction, is rather better, observers say.

For Netanyahu and Mofaz, a unity partnership with Kadima that staves off elections also potentially weakens a resurgent Labor party — or at least prevents it from boosting its Knesset presence in the near future — and might take some of the wind out of the sails of neophyte Yesh Atid party leader, ex-TV anchor Yair Lapid, who is polling at about 12 seats. While some Kadima MKs might object to the fact that only Mofaz will receive a position, they can breathe easier now that their Knesset seats are no longer threatened by national elections.

With the formation of a national unity government, Labor Chairwoman Shelly Yachimovich will likely assume the mantle of leader of the opposition.

Yachimovich denounced the deal as “an alliance of cowards and the most ridiculous and ludicrous zigzag in Israeli political history.”

Meretz Chairwoman Zehava Gal-On called the formation of the national unity government “an odious act” and said that Netanyahu and Mofaz had sent a disgraceful message to the public.

Yair Lapid, who must now wait until November 2013 to enter the political fray, scorned the deal as “old politics, corrupt and ugly… politics of seats instead of principles, of jobs instead of the public good, the group’s interests instead of the entire country. This disgusting political alliance will bury all of its members beneath itself.”

Earlier on Monday, Netanyahu had announced at a cabinet meeting that new elections would be held on September 4, saying the next government would have to deal with Israel’s core issues.

Mofaz, who just last month became the head of the opposition, had pledged not to join the government.

“I intend to replace Netanyahu,” Mofaz had told The New York Times after his resounding victority over Tzipi Livni. “I will not join his government.”