Beji Caid Essebsi, who has died aged 92, was the first freely elected president of modern Tunisia. He played a vital role in helping ensure that, more than any other Arab state, the north African country preserved many of the essential gains of the Arab spring movement.

In toppling their long-established dictator, Zine al-Abidine Ben Ali, in 2011, Tunisians led the way in what, for a time, appeared to be an unstoppable march towards democratic accountability across the region. Ben Ali’s demise was soon followed by Hosni Mubarak’s in Egypt and would eventually include Muammar Gaddafi in Libya. Even Bashar al-Assad in Syria initially faced a pro-democracy backlash. But while any hopes of an orderly transition soon foundered in those three countries, Tunisia alone – despite the scourge of terrorism at home and the appeal of jihadism to its young men – managed to establish a functioning democracy.

Essebsi, nicknamed “the old wolf”, was already 84 when he found himself at the centre of the revolutionary upheaval in Tunis. A month after Ben Ali’s flight to Saudi Arabia, Essebsi was summoned out of retirement by the acting president, Fouad Mebazaa, to serve as prime minister, a post he would hold until elections at the end of 2011 brought the moderate Islamist Ennahda party to power.

Essebsi was an unlikely, and for many a controversial, choice to help entrench a new political system: not only had he been a loyal aide to the autocratic Habib Bourguiba, Tunisia’s “founding father” and first president after independence from France, but he had – at least initially – been an ally of the by now reviled Ben Ali, who had removed Bourguiba in 1987. Despite these associations, Essebsi’s reputation as a patriot and a technocrat, and the fact that he had spent the previous two decades in retirement, meant that opposition to his new prominence could be ignored.

Serving as prime minister for the crucial year after what became known as the jasmine revolution (after the national flower) might have been enough for most politicians of such an advanced age. But in April 2012, five months after stepping down, Essebsi announced the formation of Nidaa Tounes (Tunisia’s Call), a “big tent” secularist party opposed to the Islamists and to what he saw as the extremism and violence threatening public liberties. Controversially it included former supporters of Ben Ali. Despite this, Nidaa Tounes won the parliamentary election held under a new constitution in October 2014.

Two months later, at the age of 88, Essebsi convincingly defeated the incumbent, Moncef Marzouki, a veteran human rights activist backed by Ennahda, to become Tunisia’s president.

While Essebsi had considerable success in holding together the fragile political process, his presidency was overshadowed by atrocities carried out in the name of Islamic State, with the resultant disastrous effect on tourism and the Tunisian economy. First came the attack on the Bardo Museum in Tunis in March 2015, in which 21 people, including many foreign tourists, were killed; then, in June, the deadly assault on a beach resort near Sousse in which 38 people, mostly Britons, lost their lives; and in November that year a suicide bombing targeting a bus containing presidential guards, with 12 fatalities. Two suicide attacks last month resulted in the death of a police officer.

On top of this, the Tunisian authorities had little success in ending the country’s unenviable reputation for being one of the world’s biggest exporters of jihadists. They had to contend with militant groups in remote desert areas on the border with Algeria, and high unemployment continued to stoke unrest. In April this year Essebsi said that he did not intend to stand for re-election this November, wanting to make way for someone younger.

Born in the northern coastal town of Sidi Bou Said, he came from a family of wealthy landowners. In 1941, with Tunisia loyal to Vichy France, he joined the youth wing of the Neo-Destour party, which had been founded a few years earlier to demand full independence from the French. After the war, he studied law in Paris, returning in 1952 to Tunis, where he began his career defending Neo-Destour activists. Like many of his contemporaries, Essebsi was in thrall to Bourguiba, the charismatic Neo-Destour leader, who would lead Tunisia to independence in 1956. Bourguiba was a man he would “never abandon”, even during his erratic and repressive final years in power. After joining Bourguiba as an adviser in 1957, Essebsi served, among other posts, in the key positions of head of national security and interior minister in the 1960s, a period of some domestic repression, and later as defence minister and ambassador to Paris.

His diplomatic tenure in Paris in the early 70s saw Essebsi join forces with those advocating a move to greater democracy in Tunisia. When it became clear that Bourguiba was not ready to listen, he returned home and withdrew from politics. But Essebsi rejoined the fray in the early 80s amid indications that the regime, in the shape of a new prime minister, Mohammed Mzali, was becoming more open to the idea of political pluralism. Essebsi served as foreign minister for more than five years from April 1981, a tumultuous period that included the arrival in Tunis of the Palestine Liberation Organization (PLO) after it had been driven out of Beirut, an Israeli air attack on the PLO base and America’s bombing of Libya.

After Bourguiba’s removal by Ben Ali in 1987, Essebsi agreed to represent the new order as ambassador to Bonn. From 1990 to 1991 he served as president of the national parliament. Retiring at the age of 65, he could have had little expectation of such an eventful second political career two decades later.

He is survived by his wife, Chadlia Saida Farhat, and by their sons, Hafedh, also a politician, and Khelil, and daughters, Amel and Salwa.

• Mohamed Beji Caid Essebsi, politician, born 29 November 1926; died 25 July 2019