A WEEK ago the political system fostered by the United States in Afghanistan was on the brink of collapse, with a new civil war being the likely result. After Afghan election authorities announced the preliminary results of a presidential election runoff, the apparent loser, Abdullah Abdullah, readied what looked to some like a coup, dispatching forces to Kabul police stations and lining up provincial governors to endorse his announcement of a government.

Timely phone calls to Mr. Abdullah and rival Ashraf Ghani, first by Secretary of State John F. Kerry and then by President Obama, temporarily defused the crisis. Now Mr. Kerry has brokered an accord that appears to establish a clear plan for arbitrating the dispute over the election and establishing a stable government — a turnaround so remarkable that the U.N. representative in Kabul is calling it “not just a top-notch diplomatic achievement [but] close to a miracle.

Mr. Kerry himself is rightly cautioning that “we haven’t won yet”; the delicate and complex deal must be implemented over weeks. But his weekend diplomacy in Kabul has created a real chance for a peaceful transition of power this summer, rewarding the millions of citizens who turned out to vote and creating a government that could continue to attract Western support.

The June 14 runoff at first looked like a success, as up to 8 million Afghans voted without significant disruption by the Taliban. But it soon became clear that the balloting was marred by massive fraud, particularly in the southern and eastern provinces where Mr. Ghani has his political base among ethnic Pashtuns. Mr. Abdullah, whose strongest support is from ethnic Tajiks and other minorities in the north, withdrew his recognition of the election process after releasing tape recordings implicating the chief of the election authority in ballot box stuffing.

That official resigned, but the election authority last week announced a preliminary result that gave Mr. Ghani an advantage of 1 million votes. That, in turn, prompted the near-rebellion of Mr. Abdullah. The swift intervention of Mr. Kerry and Mr. Obama was constructive. But even more important were the decisions by Mr. Abdullah, Mr. Ghani and Afghan President Hamid Karzai to act in the larger interests of the country. Mr. Karzai invited U.N. and U.S. officials to mediate the dispute; Mr. Ghani eventually agreed to an audit, under international supervision, of all of the ballots cast in the election. For his part, Mr. Abdullah agreed to accept as binding the result of the count.

U.S. Secretary of State John Kerry (L) and Afghanistan's presidential candidates Abdulah Abdullah (C) and Ashraf Ghani (R) smile together after announcing a deal for the auditing of all Afghan election votes. (Jim Bourg/Reuters)

Most crucially, the two candidates agreed that once a new president is determined, a coalition government will be established in which the loser takes a new position as “chief executive” and cabinet and patronage appointments are shared. That is essential to maintaining stability in a country that is divided along ethnic and geographical lines but has a constitution granting the president extraordinary powers. Working out the details of the power-sharing arrangement will be challenging, as will maintaining comity as the vote count proceeds. But Mr. Ghani and Mr. Abdullah appear to understand the stakes, which are nothing less than whether Afghanistan will survive as a democracy or again collapse into civil war.