Mr Ahmadinejad, a hardliner who served two terms between 2005 and 2013, filed paperwork for the 19 May poll at the interior ministry in Tehran.
Last year, Ayatollah Ali Khamenei warned him that such a move was "not in his interest and that of the country".
But Mr Ahmadinejad told reporters on Tuesday that had been "just advice".
Associated Press journalists who witnessed Mr Ahmadinejad register on Tuesday said election officials were "stunned" when he submitted the paperwork.
President Hassan Rouhani, a moderate who negotiated a nuclear deal with world powers in 2015, is yet to register but is widely expected to seek a second term.
More than 120 people, including six women, submitted their names on the first day of registration on Monday, according to local media.
Once the process ends on Saturday, the prospective candidates will be screened for their political and Islamic qualifications by the Guardian Council.
The clerical body will announce a final list of vetted candidates on 27 April.
Speaking after submitting his paperwork Mr Ahmadinejad said he was only helping his former vice-president, Hamid Baghaei, who registered alongside him.
Mahmoud Ahmadinejad's decision to register is the biggest surprise so far in the presidential election.
Ayatollah Khamenei had indicated he did not want Mr Ahmadinejad to stand and it is unusual for anyone in the political establishment to go against the supreme leader's wishes. Those who do tend to pay for it.
Hardliners have made it clear they are angry and disappointed. Some have called Mr Ahmadinejad's move "unacceptable" and said it will spell the end of his political career.
All potential candidates have to be vetted by the Guardian Council and many observers believe Mr Ahmadinejad will not pass this hurdle.
But if he does, his entry into the race is likely to take some of the shine off the hardliners' preferred candidate, Ebrahim Raisi.
It will also be damaging to President Rouhani - especially with poorer voters who have yet to feel the effects of promised economic improvements following the nuclear deal, and could be swayed by Mr Ahmadinejad's populist rhetoric and promises to bring back subsidies to help people cope with inflation.
"I registered merely to support Baghaei and I will act according to the [supreme] leader's advice," he said. "I'll be serving Mr Baghaei with all my power."
He added: "Some people say that the [supreme leader's] advice was meant to completely forbid [me from running], but what the leader said was just advice... I am still committed to my moral promise."
Ayatollah Khamenei, who holds ultimate power over Iran's civil and religious affairs, said he had told him not to stand because it would "create bipolar opposites and divisions in the country which I believe is harmful".
The disputed re-election of Mr Ahmadinejad in 2009 triggered the biggest protests in Iran since the Islamic Revolution in 1979.
Millions of people demanded a re-run, but the supreme leader insisted the result was valid and ordered a major crackdown on dissent that saw dozens of opposition supporters killed and thousands detained.
Analysts said Mr Ahmadinejad might be trying to put pressure on the Guardian Council not to disqualify Mr Baghaei, as it did with his former chief-of-staff in 2013.
Mr Baghaei was detained for seven months in 2015 on unspecified charges. In March, the judiciary said the case was still open, Reuters news agency reports.

Johnson helped the Robins avoid relegation last season after joining them from Barnsley in February.
The 35-year-old, who played 199 games for City between 2006 and 2012, started his managerial career with Oldham in 2013, before moving to Oakwell in 2015.
His previous contract at Ashton Gate was set to end at the end of the 2018-19 season.
"Like I said when I was appointed, Bristol City is the perfect club for me," he told the club website.
"We want to deliver success; everything about the club is progressive and that's why I'm desperate to be a part of that in the long term."
Bristol City have won just one of their past six games in all competitions, but they are 11th in the Championship table, just four points outside the play-off places.
Former President Mahmoud Ahmadinejad has registered as a candidate in Iran's presidential election, despite being told not to by the Supreme Leader.
[NEXT_CONCEPT]
Bristol City head coach Lee Johnson has signed a new contract to stay with the Championship club until 2020.