Anwar Ibrahim has seen it all. He has seen prison, he has seen betrayal, he has seen courtrooms, he has seen the highest office and opposition exile. And last week, emerging from prison after serving a second jail sentence for sodomy – charges which have been confirmed as trumped up and politically driven – he saw freedom in an entirely new light. “I’ve always talked about democracy, freedom, liberal ideas, but there is a difference when you taste it: you value these ideals more,” said Anwar. “When it is denied to you, freedom is a torture and also a reason for survival.”

Speaking to the Observer exclusively at his home in Kuala Lumpur, days after being released, the 70-year-old former deputy prime minister, who has long been the face of reform and hope in Malaysia, is relaxed and serene.

Certainly he is riding high both on his new-found freedom, following a pardon from the king for a “miscarriage of justice”, but also because, for the first time in his life, Malaysia has embraced his reformasi (reform) agenda. In an election which has featured a gripping mixture of betrayal, alleged corruption and the unseating of an authoritarian regime, Malaysia has found itself with its first opposition government since independence in 1957.

This was all the more extraordinary because the opposition was led by 92-year-old Mahathir Mohamad, who was Malaysia’s longest-serving prime minister, in post between 1981 and 2003. Back then he was head of the UMNO party, part of the ruling Barisan Nasional coalition, and his golden boy and heir apparent was Anwar, the charismatic reformer who advocated for a pluralistic, democratic form of Islam.

That all changed in 1998 when Anwar dared to challenge Mahathir over his penchant for awarding huge state contracts to friends, leading to allegations of cronyism. Anwar was sacked, and then, thanks to Mahathir, found himself on trial for sodomy.

The trial was an exercise in humiliation – semen-stained sheets brought out, male witnesses (who later admitted to being pressured into testifying) accusing Anwar of forcing them into sexual acts. Anwar, who had a young family at the time, was found guilty and sentenced to 15 years in jail.

He spent six years in prison, in solitary confinement and denied access to his family, before he was released in 2004.

He joined the opposition, formed his own party, the PKR, and when he was finally allowed back into politics ran against the ruling coalition, then led by Najib Razak, in the 2013 election. He lost, but won the popular vote, and Najib panicked. As Anwar puts it: “I never supported him, I had strong views against him and he took it very personally back then, which is why he wanted to finish me off.”

Facebook Twitter Pinterest Mahathir Mohamad gives a press conference following his re-election as prime minister. Photograph: STR/EPA

Using the judiciary, Najib ensured that Anwar was once again tried for sodomy. So in 2015, Anwar was back behind bars once again. It was not easy, Anwar said, to be in prison and know “if the 2013 elections had been free and fair, we would have won and I would not be in jail.”

Was he not filled with anger at the system? “Yes,” he concedes. “But after you have experienced jail for a long time, after so many years, you don’t really have that bitterness. I’m not pretending to be this great humanitarian, merciful person, but honestly I didn’t feel bitter. In the end you philosophise and just accept the unfolding drama.”

Yet no element of the elections has been more unexpected, and more turbulent, than the reconciliation of Anwar and Mahathir this year. And there was no one it took by surprise more than Anwar himself. After all, their fallout was more than political: it was personal and vindictive.

When Anwar was fired as deputy prime minister in 1998, he was told to move out of the official residence immediately. He asked for a few days as he had young children: Mahathir ordered for the water and electricity to be cut off.

Standing outside the court after he was charged with sodomy in 1999, Anwar described Mahathir as “a coward who will not seek responsibility for his own evil … Mahathir’s lust for power is insatiable”. In return, when Anwar was in jail, Mahathir made sure visiting requests from his young family were denied. And even after Anwar got out in 2004, Mahathir continued his tirade. “Imagine having a gay prime minister. Nobody would be safe,” he said in 2005. Anwar unsuccessfully tried to sue Mahathir for slander as a result.

What do you do when your so-called enemy comes and says, ‘let’s be friends and forget the past?' Anwar Ibrahim

Anwar said he was extremely suspicious when, in January, Mahathir asked to visit him in jail in the hope of reconciliation and partnership. “It was very difficult for me and initially I said to Mahathir: ‘Why would I want to have anything to do with you any more. I will forgive you, but goodbye: that’s it’,” said Anwar. “But after we talked and knowing the man as I do – filled as he is with self-confidence, self-indulgent at times – suddenly coming to see me, his nemesis, in prison, was a sign that he was really desperate or he had really mellowed quite a bit. And that was precisely what had happened.”

But his children, who had lost their father for most of their childhoods, were not so open to reconciliation (Nurul Izzah, Anwar’s daughter, is now a prominent politican in the opposition in her own right). “My children refused to participate, and were in tears in the corner,” said Anwar. “They couldn’t understand why I would meet this man who made their life hell. They disagreed with me, told me I should not make a deal with Mahathir, said to me ‘you suffered, we all suffered, because of him’.

“But I told them ‘what do you do when your so-called enemy comes and says, “let’s be friends and forget the past?”’ It is very difficult to say no.”

Nonetheless, Mahathir has still failed to apologised for what he put Anwar’s family through, though he did say that he should not have fired Anwar, which “coming from Mahathir, is good enough for me”.

Even in prison, Anwar started to have an inkling that the tides of Malaysia were turning in favour of the opposition in the election: once-hostile prison guards and doctors began to be friendly to him, slipping him phones, and finally coming in on election day to whisper that they had voted for the opposition.

While Anwar will run as an MP in the next few months, he will not serve in Mahathir’s cabinet, and there is a sense he wants to keep himself somewhat distant from the government should it not prove to follow through on all promises for reform.

Anwar’s wife, Wan Azizah, currently serving as deputy prime minister, is also keeping a watchful eye on Mahathir.

However, he has not lost his desire to be prime minister and has his sights set on office in two years – after all, as he himself says, he and his family have paid a “high price, probably too high a price” for his reformist beliefs, and wants to finally put them into practice.

Yet in the meantime Anwar is unwavering in his belief that Mahathir is fully behind the opposition reform agenda, and also has no doubts that he will hand over power in two years – a confidence probably helped by the fact that while Mahathir seems to be in perfect health, he is nonetheless in his mid-nineties. Already in their meetings, Anwar says, they are “slipping back into old ways”, and can now joke with each other about their 20-year dispute.

Quick guide 1MDB Show Hide The scandal 1Malaysia Development Berhad, was set up in 2009 to promote economic development. The Malaysian prime minister, Najib Razak, chaired its advisory board and, according to the US Justice Department, top executives and associates of Najib looted $4.5bn from the fund between 2009 and 2014, laundering it through the US, Singapore, Switzerland and other countries. Hundreds of millions landed in Najib's bank account, though he denies any wrongdoing. Where the money went The US Justice Department is seeking to recover money from the fund it says was gambled in Las Vegas, used to buy hotels, apartments, a luxury yacht, a jet, diamond jewellery, art works and to finance Hollywood films including the Wolf of Wall Street and Dumb and Dumber To. The investigation US prosecutors have alleged layers of foreign bank accounts and shell companies were used to launder the money and named Low Taek Jho, a friend of Najib’s stepson, as a key figure in the conspiracy. In one email he wrote "Looks like we may have hit a goldmine" after organising a 1MDB deal that would later allegedly become a money laundering vehicle. Singapore has fined eight banks for failing to carry out proper anti-money laundering measures in relation to 1MDB and given prison sentences to several bankers. Political impact A parliamentary inquiry found many irregularities but had no mandate to prosecute so, outraged by the scandal, 92-year-old former leader Mahathir Mohammad came out of political retirement and opposition united behind him for the elections. The government recently passed a "fake news" law that could be used to further stifle reporting on the case within Malaysia.

Their joint determination to pursue Najib for the corruption he is accused of carrying out in office is also a uniting factor. An investigation into the 1MDB scandal, where, under Najib, over $4bn was embezzled out of a government fund, with an alleged $681m ending up in Najib’s personal bank account, is already under way, and Malaysia has been gripped by a raid on Najib-linked properties which has seen 284 Hermes handbags and 72 bags of cash, watches and jewellery seized.

After all that happened between Anwar and Najib, it was also a surprise to him on election night to be handed the phone by a prison guard and told it was the prime minister on the phone with a request: Najib was refusing to accept he had lost the elections, could Anwar help them convince him to concede?

Despite all of their history, Anwar agreed to speak to Najib, and on the other end of the phone found a man who was “so, so scared. He was shattered and in complete denial.” Even after a second phone call telling Najib to concede for the sake of democracy and the country, he said a devastated Najib still could not accept defeat.

But with heavy irony, just as Anwar leaves jail, Najib appears to be facing serious legal trouble. Does he have any advice for the former prime minister? Anwar smiles slyly. “Have good defence lawyers,” he said with a grin. “And express remorse.”