After serving a maximum two consecutive terms, Rafsanjani played an important role in the election of the reformist Mohammad Khatami, who succeeded him as president from 1997 to 2005. (AFP)
Iran's former president Akbar Hashemi Rafsanjani, who died on Sunday, was a pivotal figure in the foundation of the Islamic republic in 1979 and he continued to shape it.
The pugnacious ayatollah, who was 82, was pronounced dead at a hospital in northern Tehran after suffering a heart attack, news agencies reported.
Rafsanjani's presidency, a breathing space after the end of the 1980-88 Iran-Iraq war, was marked by reconstruction, cautious reform and repairs to Iran's relations with its Arab neighbours.
But it was also marred by human rights violations, rampant inflation and difficult relations with Europe, not least with Britain after the "death sentence", or fatwa, handed down to writer Salman Rushdie by Ayatollah Ruhollah Khomeini, the founder of the revolution.
After serving a maximum two consecutive terms, Rafsanjani played an important role in the election of the reformist Mohammad Khatami, who succeeded him as president from 1997 to 2005.
Rafsanjani was thrashed in the 2005 presidential election by hardliner Mahmoud Ahmadinejad in a conservative backlash.
Rather than retreating from public view because of that humiliation, he remained in the limelight, emerging as a moderate counter-figure to the ultra-hardliners clustered around Ahmadinejad, under whom Iran's relations with the West plummeted.
But his intervention at Friday prayers after Ahmadinejad's disputed re-election in 2009 ultimately saw him slip outside the regime's most inner circle, being dubbed a traitor by some conservative foes.
Speaking after authorities had launched a crackdown that killed some of those who protested against Ahmadinejad's contested poll victory, Rafsanjani condemned the use of lethal force.
Sidelined but influential
"Today is a bitter day. People have lost their faith in the regime and their trust is damaged," he said at the time, sympathising with the families of the dead and attacking the lack of freedom of expression.
"It is necessary that we regain people's consent and their trust in the regime," he said in an explosive speech that sparked more demonstrations and another crackdown by security forces.
Rafsanjani was never seen again giving the Friday prayer sermon, the highest spiritual event of the week since the revolution.
A major test of how successful Rafsanjani's recovery was came in the 2013 presidential elections, seen as a confrontation between hardliners and more moderate forces, whom he supported.
His candidature was controversially rejected on the basis of his age.
The following year he delivered crucial support for the eventual winner, Hassan Rouhani, a moderate with whom he had a warm rapport.
He was an important backer of the deal Rouhani struck with world powers for sanctions to be lifted in exchange for curbs on Iran's nuclear programme.
Rafsanjani was always a member of Iran's top clerical body, the Assembly of Experts, charged with appointing -- and if required dismissing -- the country's supreme leader. He chaired the influential committee for several years.
He also held the chairmanship of Iran's main political arbitration body, the Expediency Council, since 1990, when he was appointed by Khomeini's successor and current supreme leader, Ayatollah Ali Khamenei.
Rafsanjani remained influential in his latter years, though his political journey embodied the risks posed by standing out and challenging accepted convention among Iran's power elite.
A political kingmaker
Rafsanjani never shied away from contrary statements, including endorsing relations with the United States -- the "Great Satan" as the founder of the revolution Ayatollah Ruhollah Khomeini called it.
He also caused a stir by saying in one of his memoirs -- already a six-volume magnum opus -- that Khomeini wanted to drop the mantra of "Death to America".
When Rouhani succeeded in clinching the nuclear deal with the US and other world powers, Rafsanjani hailed the diplomacy for ending years of tensions.
"The (domestic) warmongers think our dignity depends on getting world extremists to like us, which is always easy to achieve," he said of the principle of negotiation.
The country should interact and "participate in world affairs," he argued.
Rafsanjani, son of a wealthy pistachio farmer, had impeccable revolutionary credentials: he studied theology in the Shiite clerical nerve centre of Qom, was frequently arrested by the shah's secret police and became an early follower of Khomeini.
This masked his more pragmatic side.
His daughter Faezeh Hashemi was a leading champion of women's rights and a magazine publisher who became instrumental in helping women take part in competitive sports reversing post-revolution bans.
But there was controversy also.
Faezeh spoke out in favour of the pro-democracy Green Movement after the 2009 tumult. She spent six months in jail after later being convicted of committing propaganda against the regime.
And Rafsanjani's son, Mehdi, was sentenced to 10 years in prison for financial and security crimes in 2015, although he insisted his conviction was politically motivated.
While all his offspring have taken positions in public life, his wife Efat Marashi -- the couple had five children -- has remained in the shadows. All survive him.