JAKARTA, Indonesia — For a while, it looked as if Indonesia’s bad old days had returned.

The Constitutional Court was hearing an appeal by the losing presidential candidate, a former army general and son-in-law of Indonesia’s former dictator, who charged that the election last July had been rigged and should be overturned.

Outside, his supporters clashed with the riot police and tried to storm the court building. The police fired water cannons and tear gas.

But when the justices issued their ruling denying the appeal last month, something strange happened: The losing candidate grudgingly accepted defeat.

The most competitive presidential election in Indonesian history had come to a dramatic and peaceful end. Next month, Joko Widodo, the governor of Jakarta, will be sworn in at the Parliament building, completing a stunning rise from a child of the slums and carpenter to leader of the world’s fourth-most-populous nation.