Tehran (AFP) - Former Iranian president Akbar Hashemi Rafsanjani, a pugnacious moderate who survived for decades despite challenging his own turbaned elite, died on Sunday after suffering a heart attack.

Rafsanjani, who was 82, was a pivotal figure in the foundation of the Islamic republic in 1979, and served as president from 1989 to 1997.

"Despite the efforts of the doctors he died" at Shohadaa Hospital in northern Tehran, said Reza Soleimani, a speaker of the Expediency Council, Iran's main political arbitration body which Rafsanjani chaired.

The government announced three days' of mourning, making Tuesday a public holiday for his burial, according to the official IRNA news agency.

Heartfelt tributes poured in from Iran's leadership, including the all-powerful Ayatollah Ali Khamenei, despite the differences between the two.

"The loss of my companion of struggle, whose cooperation with me dated back 59 years, is difficult and overwhelming," the supreme leader said in a statement.

"The different opinions and interpretations at times in this long period could never entirely break up the friendship" between us, Khamenei added.

President Hassan Rouhani, who was reportedly at Rafsanjani's bedside in the hospital, also expressed his grief in a tweet.

"The soul of the great man of the revolution and politics, the symbol of resistance and perseverence, ascended to heaven," he wrote.

Rafsanjani's death is a huge loss for both reformists and moderates, as he stood as a pillar for the two camps.

- Outpouring of emotion -

All state television channels reported on the death with black ribbons across their screens, as top officials and the public joined in an outpouring of emotion.

A crowd of people had formed outside Shohadaa Hospital, despite police warnings, blocking Tehran's Valiasr Avenue, media reported.

Rafsanjani's body was later transferred to Jamaran, a religious hall in northern Tehran run by the now reform-minded family of Ayatollah Ruhollah Khomeini, the founder of the Islamic republic.

Story continues

A sobbing Foreign Minister Mohammad Javad Zarif and reformist former president Mohammad Khatami were among those seen at Jamaran in pictures posted on social media.

Rafsanjani was born on August 25, 1934 in the village of Nough in southern Iran into a wealthy family.

He studied theology in the holy city of Qom before entering politics in 1963 after the shah's police arrested Ayatollah Khomeini.

A confidant of Khomeini, Rafsanjani was the speaker of parliament for two consecutive terms until Khomeini's death in 1989.

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 Khomeini.

- Moderate counter-figure -

After serving a maximum two consecutive terms, Rafsanjani played an important role in the election of the reformist Khatami, who served as president from 1997 to 2005.

Rafsanjani sought a return to the presidency in 2005 but lost to hardliner Mahmoud Ahmadinejad in a conservative backlash.

It was a bitter defeat, but rather than retreating from public view, he remained in the limelight.

Rafsanjani emerged as a moderate counter-figure to the ultra-hardliners clustered around Ahmadinejad -- under whom Iran's relations with the West plummeted -- and criticised the crackdown that followed Ahmadinejad's disputed re-election in 2009.

In recent years though, his influence within state institutions had waned.

In 2013, his candidacy for the presidential election was rejected because of his advanced age.

The next year he delivered crucial support for the eventual winner, Hassan Rouhani, a moderate with whom he has 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.

Rafsanjani chaired the influential committee for several years.

He also held the chairmanship of the Expediency Council, since 1990, when he was appointed by Khomeini's successor as supreme leader, Ayatollah Khamenei.

The supreme leader will have to appoint a new chairman for the body.