Indonesia's presidential election may prove to be a more severe test of its young democracy than initially thought. Early tallies from Wednesday's vote give the victory to Jakarta Governor Joko Widodo over Suharto -era general Prabowo Subianto. But Mr. Subianto, a self-styled strongman who has pursued the presidency for a decade, isn't conceding. The situation could remain unresolved until late August—which means that thuggery and corruption could mar an election that until now has been largely peaceful and fair.

With 190 million eligible voters spread across 900 inhabited islands, Indonesia's election commission won't have official results until July 21 or 22. But five independent firms have released "quick counts" based on public tallies at a sample of polling places nationwide. These estimates have been accurate in the past, and all now have Mr. Widodo winning by three to six percentage points.

Yet at least two media outlets owned by Subianto allies disagree. So while the Widodo camp has celebrated in the streets, Mr. Subianto has claimed his own "mandate" and refused to concede defeat.

This alone isn't problematic. Especially in the closest election Indonesia has seen, Mr. Subianto is entitled to wait for official results and even to ask the constitutional court for a review. In 2009 Megawati Sukarnoputri —today Mr. Widodo's patron and party chief—got the court to review a vote she lost by 34 points.

Joko Widodo Agence France-Presse/Getty Images

The question is whether Mr. Subianto will respect the system's verdict. There is reason to worry. In Suharto's final years, then-General Subianto was the strongman's son-in-law and possible heir, and his bloody assaults on democracy activists eventually got him expelled from the army. A descendant of Javanese royalty, he denigrates democracy even while running for office, saying last month that direct elections "are not in accordance with our own culture." And now he faces defeat at the hands of Mr. Widodo, the self-made son of a carpenter admired for his common touch.

Certainly Indonesia doesn't appear to be at risk of a military coup a la Thailand. The armed forces have no recent record of such direct political intervention, and some of Mr. Subianto's harshest critics (and Mr. Widodo's strongest supporters) are senior military and intelligence veterans.

Yet the army has acknowledged the dangers of unrest in case of a close election, with Chief of Staff General Budiman saying last weekend, "If the difference if slim, we have to watch out." That isn't reassuring, especially when Mr. Subianto is backed by vigilante thugs such as the Islamic Defenders Front, which opposes both religious pluralism and democracy. Corruption is also a risk: Last month the constitutional court's former chief justice received a life sentence for manipulating electoral disputes in exchange for bribes.

This electoral uncertainty highlights Indonesia's accomplishments as well as its challenges. Consolidating democracy is never easy, but in 16 years the world's fourth-largest country—and the most populous with a Muslim majority—has already seen several peaceful transfers of power. Investors who have bid up Indonesian stocks and currency since Wednesday are betting on a stable transition in the weeks ahead, and we hope they're right.