Flanked by reporters after filling out registration forms and making a victory sign, Ahmadinejad said: "The Leader advised me not to participate in the elections, and I accepted".
One stunned official told the AP that the move amounts to nothing less than "an organized mutiny against Iran's ruling system".
Ahmadinejad had insisted he would not run after Khamenei said previous year that his candidacy would have a "polarizing" effect on the nation, and instead backed his ex-deputy Hamid Baghaei. Persian social media erupted on 6 April when Raisi - the favourite candidate to replace the Supreme Leader, Ayatollah Khamenei - announced his intention to run for president.
The Guardian Council, a powerful body of 12 clerics, has to approve the eligibility of the candidates before their candidacy is made final. Iran insists its nuclear programme is for peaceful purposes.
In a move to widen Mr Ahmadinejad's support, his senior aide Esfandiar Rahim Mashaie in May sent a conciliatory letter to Mr Ahmadinejad's predecessor as president, Mohammad Khatami, seen as the father of Iran's reform movement.
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".
But Ahmadinejad said today that he interpreted that statement not as an order, but as advice. He will pose a significant challenge to the reelection of popular moderate President Hassan Rouhani.
That sets up a potential clash for Khameini, who ultimately will decide the victor of the election and who could be forced to choose between two unappetizing options: The stability of current President Hassan Rouhani, who's relatively liberal voice has often frustrated the Supreme Leader, and alienating a large number of constituents who view Ahmadinejad's tough talk as a source of Iranian pride.
"Ahmadinejad knows full well that his candidacy is an affront to Khamenei who had publicly barred him from running", said Mr Vaez. Baghaei was jailed for unclear reasons for several months in 2015, and Ahmadinejad's high-profile stunt may have been a way to put pressure on the Guardian Council not to disqualify him. The council normally does not approve dissidents or women for the formal candidate list.
Ahmadinejad was in office from 2005-2013, but his 2009 win came under a cloud of vote-rigging accusations.
The registration process is to last for three more days and Iranians will go to the polls on May 19. Under that deal, Iran agreed to curb its uranium enrichment in exchange for the lifting of global sanctions.