"The country's fate is determined by the people."
Polls close at 6 p.m. (1330 GMT), although authorities often extend voting into the evening. Ballot counting will start at midnight and final results are expected within 24 hours of polls closing, the semi-official Fars news agency said. The elections are also for city and village councils.
In a warning reflecting rising political tensions amid signs of an unexpectedly close race, Rouhani urged Iran's powerful elite Revolutionary Guards, believed to support Raisi, not to meddle in the election.
Suspicions that the Guards and the Basij militia under their control falsified voting results in favour of hardliner Mahmoud Ahmadinejad led to eight months of nationwide protests in 2009. Dozens of people were killed and hundreds arrested, human rights groups say, in the worst unrest to hit the Islamic Republic.
Raisi, 56, and Rouhani, 68, traded charges of graft and brutality on live television with an open vehemence unseen since the 1979 Islamic Revolution. Both deny the other's accusations.
"From the Revolutionary Guards to Friday prayer leaders, the hard-line, unelected part of the establishment backs Raisi," a senior former Iranian official told Reuters.
"But it is a risky decision. It might cause protests similar to those in 2009, as different walks of the society, desiring evolution inside the establishment, have united against Raisi."
The Guards hope that a win for Raisi will give them an opportunity to claw back economic and political power lost in Shi'ite Iran's complex theocratic and republican governing structure since 2015, when Iran struck a nuclear deal with world powers that brought it out of international isolation.
POSSIBLE SUCCESSOR TO KHAMENEI
Although Khamenei is guarded about his political preferences, he appears to back Raisi both as a presidential candidate and possible successor.
But in an apparent reference to the 2009 disturbances, Khamenei, an unelected clerical hardliner who has the ultimate say in Iran, has previously warned he would confront anyone trying to interfere in the election.
Rouhani, who championed the deal to lift most sanctions on Iran in return for curbs on its disputed nuclear program, is a staunch supporter of engagement with the West and liberal reforms to the economy, now dominated by state institutions.
He also says a hard-line victory could put Iran back on a more confrontational, economically damaging course with the West, and would prevent the opening of society that a majority of Iranians, especially the youth, yearn to see.
But hard-line rivals hammered Rouhani over his failure to boost an economy weakened by decades of sanctions, even after most were rescinded following the nuclear agreement.
Despite the removal of nuclear-related sanctions in 2016, lingering unilateral U.S. sanctions that target Iran's record on human rights and terrorism have kept many foreign companies wary of putting stakes in the Iranian market.
A protégé of Khamenei, Raisi focused his campaign on the economy, visiting rural areas and villages, promising housing, jobs and more welfare benefits for the poor.
Analysts have rejected Raisi's promises of jobs and cash handouts as unrealistic but admit these could win traction with voters who have felt few benefits so far from the nuclear deal.
Raisi, a long-serving member of the judiciary who was one of four judges who sentenced thousands of political prisoners to death in 1988, gathered the hard-line camp behind him after other conservative candidates dropped out of the race.
But analysts agreed the gathering of Khamenei's powerful allies behind Raisi seemed to have the unintended effect of energizing Rouhani's supporters, uniting opposition and pro-reform figures, artists and activists to back his re-election.
"This is a polarized election - a race between powerful unelected centers of power and the rest of the country," said analyst Hamid Farahvashian.
Behnam Ben Taleblu, senior Iran analyst at the Foundation for Defense of Democracies (FDD) in Washington, said Rouhani's warning to the Guards about electoral interference "could be aimed at forcing the establishment to back his second term."
The campaign was replete with caustic televised debates and bruising broadsides. Raisi accused Rouhani of "economic elitism, mismanagement, yielding to Western pressure, and corruption."
PUSH FOR REFORM
Rouhani hit back in a sharper campaign strategy to mobilize Iranian women and young people who became jaded about the vote after losing hope in his ability to ease religious repression in society as promised in 2013, when he won by a landslide.
While the vote may not have a decisive influence on foreign policy, which is set by Khamenei, the election of a hardliner could harm Iran's global image and further deter foreign trade and investment seen as vital to rebuilding the economy.
Although rights campaigners say there have been few, if any, moves to bring bout greater freedoms under Rouhani, many young Iranians see Rouhani as the sole choice, even if it is one they are likely to make without real enthusiasm.
More than a third of Iran's 80 million population is under age 30 and yearns for reform. Women comprise more than half the population and have lesser rights than men in areas including inheritance, divorce and child custody.
Some 350,000 security forces were deployed around the country to protect the election, state television reported.