JERUSALEM—A last-gasp legal challenge by Israeli Prime Minister Benjamin Netanyahu to thwart a bid by a rival to head a new government was rejected on Tuesday as his opponents raced to seal a pact that would unseat him. Naftali Bennett, Netanyahu’s former defense minister, announced on Sunday he would join a proposed alliance with opposition leader Yair Lapid, serving as its premier first under a rotation deal. They have until Wednesday midnight local time to present a final pact to President Reuven Rivlin, who handed Lapid the task of forming a new government after Netanyahu failed to do so in the wake of a close election on March 23. Hoping to beat the deadline, Lapid, Bennett, and other party leaders convened to clinch coalition agreements, sources briefed on the talks said. In a letter to the legal counsels of the presidency and parliament, Netanyahu’s conservative Likud said Lapid was not …