The international community has a rare second chance to help the Maldives make a peaceful democratic transition, after the shock ousting of its authoritarian leader in Sunday’s election.

The opposition’s unassailable lead in the provisional results surprised everyone, not least the sitting president, Abdulla Yameen. Over five years he had jailed or forced into exile the entire opposition leadership and seized control of state institutions, including the judiciary and elections commission. Many of the commission’s staff were Yameen loyalists, seen cheering at his rallies. The elections commissioner himself had previously been secretary general of Yameen’s own political party. Mohamed Nasheed, deposed in a coup in 2012 and the opposition’s first choice of candidate, was barred from contesting following a spurious conviction for terrorism. Foreign journalists, meanwhile, found themselves suddenly blocked from entering the famous tourism hotspot by byzantine new visa procedures.

Under these circumstances, few countries bothered to even send election observers. But despite all these obstacles, 90% of eligible voters turned out and the opposition stormed home with 58% of the vote – a staggering democratic achievement. After a long silence and with the loyalty of state institutions crumbling around him, Yameen was forced to concede on national television. Several watchers noted he looked almost relieved.

Yameen set the Maldives on the path to pariah state, steering it into the arms of China and Saudi Arabia. His legacy will be one of violence, intimidation and corruption. A famous blogger and satirist was stabbed to death; a journalist mysteriously disappeared, with a machete lodged in the door of his news outlet. There was, Yameen told his home minister at the time, no need to be “overwhelmed” by the investigation into the disappearance. His courts freed the prime suspects last month.

Meanwhile, two years ago an al-Jazeera investigation claimed it had traced a billion-dollar money-laundering operation right to the president’s door – allegations he denied – secretly filming the bagmen complaining about the weight of the cash-filled sacks they had to deliver via moped.

Over five years, Abdulla Yameen jailed or forced into exile the entire opposition leadership and seized control of state institutions.’ Photograph: Ahmed Shurau/AFP/Getty Images

The international community has misjudged the Maldives before. It was too quick to recognise the regime that seized power and derailed the democratic transition in the 2012 coup, swayed by the UN background and Stanford education of Mohamed Waheed, who took over and paved the way for Yameen. Maldivians showed what they thought of this, awarding Waheed just 5% in the 2013 vote – among the lowest recorded result for an incumbent president.

Sunday’s result offers a new opportunity to help the Maldives back on the path to democracy. The primary concern is reforming the rubber-stamp judiciary, a legacy of the country’s 30-year dictatorship under Maumoon Abdul Gayoom. Like Gayoom, Yameen wielded the courts against his many adversaries, jailing political opponents and shielding himself from corruption inquiries under the pretence of judicial independence. Judges who failed to cooperate or sought other masters were themselves thrown in jail. Supreme court shenanigans are still likely before Yameen leaves office on 17 November.

The Maldives has a honeymoon period for judicial reform, but afterwards history may well repeat itself. The president-elect, Ibrahim “Ibu” Mohamed Solih, might represent the Maldivian Democratic party, but not all those in the opposition coalition are democrats. Yameen’s prison is a Pandora’s box of awkward decisions, holding his disgraced accomplice and former vice president Ahmed Adeeb, former defence minister Mohamed Nazim, who held the mob’s megaphone in 2012’s coup, and Gayoom himself – at 80, currently the Maldives’ oldest inmate.

The state of the courts makes fair trials an impossibility: according to one former member of the judicial services commission, many judges are poorly educated and some even have criminal records. The Maldives desperately needs outside assistance, ideally experienced foreign judges on the bench, to help restore trust and legitimacy to this stricken institution. The coalition is for now united against Yameen, but this will not last long.

The feeling for many Maldivians, meanwhile, is one of profound relief as much as celebration. An exiled journalist, Zaheena Rasheed, forced abroad after aiding al-Jazeera’s corruption investigation, expressed the sentiments of many on social media: “Guys – I’m going home.”

• JJ Robinson is the author of Maldives: Islamic Republic, Tropical Autocracy and the former editor of Minivan News