Image copyright Reuters Image caption All smiles now - but today's posters hide a story of betrayal and restitution

The release of Anwar Ibrahim is an important step in a new beginning for Malaysia, building on the stunning electoral victory that ousted the ruling party on 9 May. It's also a test of loyalty between two men with a long rivalry, one of whom was the other's jailor, writes Bridget Welsh.

The story of the relationship between just-elected Prime Minister Mahathir Mohamad and Anwar Ibrahim is so full of drama it would be fitting for Shakespeare - loyalty, betrayal, tragedy and irony meld together in a narrative for the ages.

Dr Mahathir, now 92, first put Anwar in prison on corruption and sodomy charges in politically charged circumstances in 1999. But he has now come out of retirement, won a second stint as prime minister - and provided the key to his former deputy's release nearly two decades later.

Mr Anwar himself read all the volumes of Shakespeare when in prison after Dr Mahathir jailed him. Yet the chance for Dr Mahathir to assure the release and pardon of Mr Anwar allows the relationship to come full circle, where enemies become friends.

And unlike many of Shakespeare's plays, the story is one where the villain becomes the hero.

Riding the wave

At the core of the story of the two men is political power, and the drive to secure and maintain it.

Dr Mahathir first brought Mr Anwar into his government in 1982. Mr Anwar had risen to prominence as a dynamic, charismatic student leader, who mobilised opposition to the dominant incumbent party, the United Malays National Organisation (UMNO), in the 1970s.

Media playback is unsupported on your device Media caption Anwar Ibrahim says the opposition's election victory is "a new dawn" for Malaysia.

Mr Anwar rode the rising waves of political Islam, capitalising on the space available to organise around religion and the growing international movement of Muslim empowerment surrounding the 1979 Iranian Revolution.

The co-opting of Mr Anwar into UMNO was the first of many political moves in their relationship, as Mr Anwar became a protégé who rose quickly up the ranks.

Image copyright Reuters Image caption The former protégé (left) meets the mentor who jailed him

Mr Anwar's charisma and ability to mobilise followers served to neutralise opposition to UMNO within Malaysian society. The 1980s and early 90s were a heyday for Malaysia, with rapid economic growth and gains in development.

Dr Mahathir's leadership had put the country on the international stage, in an era where Asia was seen to be rising in influence. Mr Anwar's loyalty and role were an integral part of Malaysia's success in this period.

In 1993, Mr Anwar emerged as the leading contender to replace Dr Mahathir, winning a decisive victory for himself within UMNO. At that time, Mr Anwar wrote his first book, The Asian Renaissance, picking up on the momentum and aspirations in the region and distinguishing himself as an Asian public intellectual.

He began to openly discuss the need for political reform, showcasing a difference in the two men as one who wanted to hold on to power and another who aspired to win it through calls for reform.

Crunch time

The reckoning would come after the 1997 Asian Financial Crisis. In 1998 Mr Anwar challenged Dr Mahathir for control of the party. The challenge was filled with vicious personal attacks across the political divide, as ideological struggles over policy melded with allegations of corruption.

Mr Anwar failed in his challenge, as Dr Mahathir proved adept at controlling the levers of power. Mr Anwar was beaten up, charged and convicted in events that shook Malaysia's political system to the core. Betrayal was viciously punished as power was retained at all cost. Dr Mahathir rejected calls for political reform and held on by winning the 1999 election.

Image copyright ROSLAN RAHMAN/AFP/Getty Images Image caption The brutal treatment of Anwar Ibrahim raised worldwide concern

The reformasi movement calling for change was born, with Mr Anwar as its jailed leader. The 1998-99 cataclysm was to set a pattern that has shaped political contests for more than two decades, as every election was won by the person who personified political change.

Even the mandates won by Abdullah Badawi in 2004 and then Najib Razak in 2013 were embedded with labels as reformers. Malaysians have closely identified with the need to improve governance.

Mr Anwar remained in prison until 2004, released by Abdullah Badawi who became prime minister after Dr Mahathir stepped down in 2003. While initially popular, Mr Abdullah failed to meet the expectations of the public and stakeholders within his party, and lost decisively in the 2008 election.

Dr Mahathir returned to political life to assure the political downfall of Mr Abdullah through open criticism and behind-the-scene manoeuvres within UMNO. The man who had released Mr Anwar faced Dr Mahathir's political wrath.

Image copyright AFP Image caption Can Anwar supporters and other Malaysians look forward to a new dawn?

In stepped another of Dr Mahathir's protégés - Mr Najib - who assumed the mantle of leadership in 2009. Mr Anwar was now the leader of the opposition, having successfully brought together different groups to make gains in 2008.

Mr Najib followed the Mahathir playbook and convicted Mr Anwar of sodomy in a tainted political trial. But Mr Najib made the same mistake as Mr Abdullah, failing at national leadership.

Changing sides

The 2015 1MDB scandal and ostentatious displays of wealth tied to perceived corruption provoked strong nationalistic responses by Dr Mahathir, who openly criticised Mr Najib's leadership. Mr Najib fought Dr Mahathir, engaging in personal attacks, purging him from the party and threatening charges.

Mr Najib pushed Dr Mahathir back into Mr Anwar's fold. In 2016 Dr Mahathir began collaborating with the opposition and assumed its leadership in December. An integral part of this relationship involved turning power over to Mr Anwar, after securing his release and pardon.

Media playback is unsupported on your device Media caption Malaysian voters react in 2018 to Mahathir Mohamad's victory over Najib Razak

The 92-year old steered the opposition toward victory this month, calling for Mr Najib's removal, reinstating the rule of law and political reforms. Mr Anwar's agenda of the 1990s was co-opted by the target of those criticisms, the very person who had repudiated and punished Mr Anwar for his call for change.

Today, with Mr Anwar's release, the two men are allies again. They both publicly attest a commitment to political reform.

The betrayals of the past, however, are there, and how much these are put aside for a shared goal is not clear. The timing of the turnover of power and scope of political reform are likely to emerge as points of difference, as for both men power has been central in their trajectories.

Now more than ever Malaysians hope these men focus on loyalty to the country as opposed to personal interest and work together as allies toward strengthening Malaysia.

Bridget Welsh is associate professor of political science at John Cabot University in Rome.