From the period of Ayatollah Ruhollah Khomeini’s militant opposition to the Shah’s regime in Iran in the 1970s, until his death, at the age of 82, Ali Akbar Hashemi Rafsanjani was one of Iran’s major political players. Considered a pragmatic liberal, he had at times been ruthless. His charm and immense wealth gave him a cachet that few other Iranians enjoyed. He grew close to Khomeini during the latter’s exile in the 60s and became rich as a property speculator in the following decade. He suffered imprisonment under the Shah in 1963, 1964, 1967, 1970, 1972 and 1975on and off throughout the 60s and 70s, but, after the Islamic revolution in 1979, he held various positions of state, including, from 1989 until 1997, that of president. He was credited with Iran’s reconstruction after the 1980-88 war with Iraq.

Born in the village of Bahraman, near Rafsanjan, into a wealthy family of pistachio farmers, Rafsanjani attended a traditional Qur’anic school and in 1948 went to a seminary in the holy city of Qom, where he lived near Khomeini. By 1956, he had completed his basic theological studies. In 1963, the Shah ordered seminarians, technically exempt, to serve in the army. Rafsanjani fled and was arrested.

Ayatollah Khamenei, left, with Rafsanjani at the tomb of Ayatollah Khomeini in Tehran in 2006. Photograph: Reuters

He was rumoured to have provided the gun used to assassinate the prime minister Hassan Ali Mansur in 1965. During this period Rafsanjani co-operated closely with future Islamic leaders such as the present supreme leader Ayatollah Ali Khamenei. In 1978, Khomeini appointed Rafsanjani a member of the Council of the Islamic Revolution, and he was a co-founder of the Islamic Republic party in February 1979. In May that year, he escaped an assassination attempt. In 1980, he was elected speaker of the Iranian parliament and served until 1989. From then until his death he was chairman of the expediency council. In 2007, he became chairman of the crucial assembly of experts, serving until 2011.

He often sailed close to the wind. As speaker he had confirmed in 1985 Khomeini’s appointment of Ayatollah Hossein Ali Montazeri to succeed as supreme leader. The conservatives strongly opposed this choice but Khomeini depended on Rafsanjani for political advice. Khomeini’s biographer Baqer Moin described Rafsanjani as “a witty and soft-spoken politician” who saw himself as a “patriot and a reformer who tried to advance a pragmatic interpretation of Islam, the revolution and Khomeini’s thinking”.

Brilliant at networking and wheeler-dealing, Rafsanjani was always a powerful figure. When he visited China and Japan in 1985 he was received as a head of state. It was during this visit that the US president Ronald Reagan thanked him for bringing a peaceful end to the hijacking of a TWA jet by Lebanon’s Hezbollah. This was one of many instances when he enraged the then prime minister, Mir Hossein Mousavi, by dominating foreign policy.

Rafsanjani was clever at appealing to liberals and, where necessary, hardliners alike. The hardliners were pitted against him and fellow liberals when the US arms-for-hostages scandal in 1986 was revealed in the Lebanese press. Montazeri and his relative Mehdi Hashemi were alarmed by this rapprochement with the US. The radicals drew them into a power base from which the conservatives might grasp the leadership when Khomeini died. Hashemi was arrested for trying to sabotage the negotiations with the US while Rafsanjani was forced to disclose the story of what became known as Irangate.

Rafsanjani survived where others might have fallen. He had Hashemi put on trial. Hashemi’s execution in 1987 sent a signal to the hardliners that Rafsanjani would not tolerate their interference. He then presented himself as a defender of the radicals. As Khomeini’s representative on the supreme defence council, he ran the war with Iraq and controlled the supply of weapons to the radical Revolutionary Guards. As president, he promoted a free-market economy, encouraging a World Bank-inspired structural adjustment policy. In 2005, he ran for a further term, but lost to Mahmoud Ahmadinejad in the run-off.

In 1988, he had been influential in persuading Khomeini to “swallow poison” by unconditionally accepting UN Security Council resolution 598, ending the war with Iraq. This followed Iraq’s use of chemical weapons against Iran. Rafsanjani persuaded Khomeini that using such weapons in retaliation would provoke new missile attacks against Iran, further demoralising the people and undermining the revolution. When a US warship shot down an Iranian passenger plane in July 1988, killing nearly 300 passengers, Rafsanjani helped keep tempers under control.

Nevertheless, he was ruthless when it was necessary. The newspaper Ettela’at quoted him in October 1981 as saying of the militant Mujahedin-e Khalq movement that “God’s law prescribes four punishments for them: one – kill them; two – hang them; three – cut off their hands and feet; and four – banish them. If we had killed 200 of them after the revolution, their numbers would not have mounted in this way.”

During his presidency, dissidents, drug offenders, communists, Kurds and Bahais were executed. In 1982, the former prime minister Mehdi Bazargan addressed an open letter to him accusing his government of creating an “atmosphere of terror, fear, revenge and national disintegration …”

Although after the Iran-Iraq war he sought closer ties with the west, he refused to lift Khomeini’s fatwa against the writer Salman Rushdie. He was a supporter of Iran’s nuclear programme, although he insisted that Iran did not seek weapons of mass destruction. After the end of his presidency he delivered a sermon at Tehran University in 1999 praising the government’s use of force to suppress student demonstrations.

With the passing of the years, his attitudes became softer. In a speech on 17 July 2009, he criticised restrictions on the media and the suppression of activists and stressed the importance of the popular vote. Some two million people attended this speech, which some regarded as one of the most important in modern Iranian history.

During that year’s presidential elections, the radical Ahmadinejad won a landslide victory over his reformist challenger, Mousavi. Many believed that the election was rigged. Both Khamenei and Ahmadinejad, as well as the opposition Green Movement, recognised the value of securing Rafsanjani’s support. He registered as a candidate in the 2013 presidential elections, but after he was disqualified from standing backed the moderate cleric Hassan Rouhani, the current president.

Rafsanjani is survived by his wife, Effat Marashi, whom he married in 1958, and their three sons, Mohsen, Mehdi and Yasser, and two daughters, Fatemeh and Faezeh.

• Ali Akbar Hashemi Rafsanjani, politician, born 25 August 1934; died 8 January 2017