JERUSALEM — Prime Minister Benjamin Netanyahu suffered a stunning defeat on Thursday after he failed to meet a midnight deadline to form a new government, casting a cloud over his future as prime minister and thrusting Israel into the chaos of a new election.

Just seven weeks ago, when Mr. Netanyahu basked in a postelection “night of tremendous victory,” he seemed invincible, confident that he would serve a fourth consecutive term and a fifth overall. Despite a looming indictment on corruption charges, he appeared set to surpass the nation’s founding leader, David Ben Gurion, as Israel’s longest-serving prime minister.

But after weeks of negotiations, his plans ran aground on a power struggle between two blocs of his potential right-wing coalition — the secular ultranationalist and ultra-Orthodox factions — who refused to compromise on proposed legislation on military service.

[Israel is back in election mode and in uncharted territory.]

The dream collapsed in a breathtaking display of political maneuvering in recent days, as Mr. Netanyahu, long nicknamed “the magician” for the political wizardry that has kept him in office continuously for the past decade, desperately tried to salvage his fortunes.