MALE (Reuters) - The Supreme Court of the Maldives on Thursday ordered the immediate release of former president Mohamed Nasheed and eight other opposition leaders, saying their trials had violated the constitution and international law - but it also ordered new trials.

FILE PHOTO: Maldives' former president Mohamed Nasheed speaks during a news conference in Colombo, Sri Lanka January 22, 2018. REUTERS/Dinuka Liyanawatte

The country has been mired in political unrest since Nasheed, its first democratically elected leader, was ousted in 2012. He was later sentenced to 13 years in jail on terrorism charges after a trial denounced as hasty and unfair by the United Nations human rights chief.

In its ruling on Thursday, the Supreme Court said it found that prosecutors and judges had been unduly influenced “to conduct politically motivated investigations” into the allegations leveled at Nasheed, former vice president Ahmed Adeeb and the other opposition leaders who had challenged President Abdulla Yameen.

It ordered fresh investigations and trials to be held.

It also nullified a ruling in which 12 legislators lost their parliamentary seats for defecting last July from Yameen’s ruling party. Yameen lost his majority in the 85-member legislature after the defections.

Ibrahim Hussain Shihab, a spokesman for the President’s Office, said in a statement that the government had questions about the ruling but would comply with it.

“The Administration will work to engage, and consult with, the Supreme Court in order to comply with the ruling in line with proper procedure and the rule of law.”

Attorney General Mohamed Anil said however that Yameen had dismissed the chief of police following the ruling after he had been unable to reach him to ensure law and order.

After the ruling, hundreds of opposition supporters cheered and waved national flags outside the main opposition Maldivian Democratic Party (MDP) campaign center.

Islamic prayers were also recited at the opposition rally, and people chanted: “The government is over.”

Maldives police said their commissioner - prior to his sacking by Yameen - and deputy commissioner had decided to enforce the ruling after seeking legal advice.

“Police have started work to enforce the ruling,” they said on Twitter.

President Yameen’s official website said the defense minister had called on the Maldivian people to put on a united front and work together to maintain peace and stability.

U.S. Ambassador for Maldives Atul Keshap welcomed the Supreme Court decision. “I urge the government and security services to respect this ruling, which bolsters democracy and rule of law all Maldivians,” Keshap tweeted.

Nasheed, who has been in exile in the United Kingdom but wants to contest a presidential poll this year, had sought U.N. help to restore his political rights.

The joint opposition in a statement welcomed the rulings and called for the immediate resignation of President Yameen.

“The Supreme Court’s verdict effectively ends President Yameen’s authoritarian rule,” it said in a statement.