A big turnout on Friday led to the vote being extended by several hours to deal with long queues.

Campaign pledges

Who is Hassan Rouhani?

Born in 1948 in Sorkheh in northern Iran and began studying religion at an early age

Began political career in 1960s by following the exiled leader of the Islamic movement Ayatollah Khomeini

Fled Iran in 1977 after publicly declaring Khomeini an "imam"

Returned to Iran after the 1979 revolution and served as the secretary of Iran's Supreme National Security Council from 1989 to 2005

Earned the nickname the "diplomatic sheikh" owing to his clerical background and role as Iran's chief nuclear negotiator from 2003 to 2005

Sworn in as president on August 4, 2013

During his term, Iran reached a landmark deal in 2015 to curb its nuclear programme in exchange for sanctions relief

The election was seen by many as a verdict on Rouhani's policy of opening up Iran to the world and his efforts to rebuild its stagnant economy.

Rouhani swept into office four years ago on a promise to reduce Iran's international isolation.

Friday's poll was the first since he negotiated a historic deal with world powers in 2015 to curb the country's nuclear programme in exchange for sanctions relief.

In the campaign trail, Rouhani sought to frame the vote as a choice between greater civil liberties and "extremism", criticising the continued arrest of reformist leaders and activists.

Raisi, for his part, accused Rouhani of mismanaging the economy and positioned himself as a defender of the poor, calling for a much tougher line with the West.

Political commentator Mostafa Khoshcheshm said that in contrast to the 2013 election campaign, when Rouhani spoke about the removal of sanctions and the improvement of the economy, this time his message was different.

"He resorted to other campaign slogans, like [calling for] social and political freedom, and he pushed the boundaries in order to gather public support, especially in large cities," Khoshcheshm told Al Jazeera.

"If he has secured this result, it's because of the large cities and the middle-class society living there - they have voted for him and made him a president and they expect him to do his promises."

Obstacles ahead

Rouhani's re-election is likely to safeguard the 2015 agreement, under which most international sanctions have been lifted in return for Iran curbing its nuclear programme.

Foreign policy plays major role in Iran election

Rouhani has vowed to work towards removing the remaining non-nuclear sanctions, but critics argue that will be hard with Donald Trump as US president. Trump has repeatedly described it as "one of the worst deals ever signed", although his administration re-authorised waivers from sanctions this week.

Rouhani is also expected to face the same restrictions that prevented him from delivering substantial social change in his first term.

Supreme Leader Ayatollah Ali Khamenei has veto power over all policies and ultimate control of the security forces, while Rouhani has been unable to secure the release of reformist leaders from house arrest.

Al Jazeera's Jonah Hull, reporting from Iran's capital, Tehran, said that Rouhani, during an "increasingly acrimonious election campaign, alienated a lot of Iran's significant state institutions who may be in no mood to cooperate with him going forward".

Slow pace of change

While the nuclear deal was at the forefront of the election, the campaign was dominated by the issues of poverty and unemployment.

Rouhani has brought inflation down from around 40 percent when he took over in 2013, but prices are still rising by more than seven percent a year.

Oil sales have rebounded since the nuclear deal took effect in January 2016, but growth in the rest of the economy has been limited, leaving unemployment at 12.5 percent overall - close to 30 percent for the young - and many more are underemployed or struggling to get by.

"Rouhani now gets his second term, and will be able to continue the work that he started in his first four-year term trying to reform Iran," Hull said.

"And moving on, crucially, from the nuclear deal to try and bring much more economic progress to satisfy the people who have found themselves extremely disappointed with the very slow pace of change since that agreement was signed."