The Maldives’ top court has cancelled the 13-year prison sentence of former President Mohamed Nasheed following a review of his controversial conviction on terrorism charges.

Nasheed, who recently returned home after years in self-exile, was elected president in 2008, becoming the island nation’s first democratically chosen leader.

But he was toppled in what he called a coup in 2012, and found guilty of “terrorism” three years later over the arrest of a top judge during his tenure.

The Supreme Court ruled on Monday that 51-year-old was wrongfully charged and the criminal court should not have proceeded to trial.

Nasheed welcomed the news in a post on Twitter, expressing his “deepest gratitude to the people of the Maldives”.

The ruling came 10 days after the inauguration ceremony of recently elected President Ibrahim Mohamed Solih, who defeated former leader Abdulla Yameen in a September 23 vote.

The presidency of Yameen, who assumed office in 2013 after winning a controversial runoff against Nasheed, was marked by political intrigue, turmoil and allegations of corruption.

Nasheed’s trial in 2015 was declared unfair by the United Nations, prompted widespread protests and resulted in the arrest of hundreds of dissidents.

In the years that followed and until the recent elections, almost all key opposition leaders were either jailed or gone into exile. In 2016, Nasheed also sought political asylum in the UK after travelling there on medical leave from prison.

Speaking after the court’s ruling, Hisaan Hussein, Nasheed’s lawyer, called the former president’s “entire trial … a politically-motivated sham”.

“It is appalling that an innocent man was unjustly forced to spend a year in jail, 35 months in exile, and was prevented from standing for political office.”

Nasheed was expected to contest the September poll but was barred on account of his terrorism conviction.

Pres. @MohamedNasheed was today finally cleared of terrorism charges. SC in delivering its judgement noted tht PN was wrongfully charged& tht it was wrong of Crim Crt to accept the charge & proceed with the case.The Court struck down the terrorism charge & squashed his conviction — Hisaan 🎈؟ (@hisaanhussain) November 26, 2018

Nasheed’s party, the Maldivian Democratic Party (MDP), also welcomed the ruling.

“The MDP has always maintained that President Nasheed’s arrest, trial, conviction, and detention were politically motivated.”

Former President Maumoon Abdul Gayoom, who ruled the Maldives for 30 years but lost the country’s first multiparty election to Nasheed, also took to Twitter to say that “justice has prevailed”.

I am so happy that President @MohamedNasheed has been freed by the Supreme Court. Justice has prevailed. Congratulations and good luck! — Maumoon Abdul Gayoom (@maumoonagayoom) November 26, 2018

Many others also took to Twitter to express joy at the ruling.