His strong rhetoric against the United States and Israel and unbending stance on Iran's nuclear programme has proved popular at home but has enraged the West.
In 2009, international criticism intensified when his re-election caused his country's worst political unrest since the 1979 Islamic Revolution.
The results, disputed by defeated reformists and moderates who demanded a rerun, led to violent anti-government protests.  They were crushed but a deep rift was left within Iran's political and clerical elite.
Supreme Leader Ayatollah Ali Khamenei, who has the final say in matters of state, declared the vote valid so that Mr Ahmadinejad could be sworn in for a second term.
The president is among a younger generation of lay politicians who are fiercely loyal to him.
The son of a blacksmith, Mr Ahmadinejad was born in 1956 in Garmsar, near Tehran, and holds a PhD in traffic and transport from Tehran's University of Science and Technology, where he was a lecturer.
He was not well-known when he was appointed mayor of Tehran in 2003.
While running the city he reduced social freedoms and curtailed many of the reforms introduced by more moderate figures who ran the city before him.
His rise to power and landslide victory in 2005 surprised the international community, which anticipated a win for the incumbent president Ali Akbar Hashemi Rafsanjani.
There has been confusion about Mr Ahmadinejad's role in the 1979 Islamic Revolution.
Several of the 52 Americans who were held hostage in the US embassy in the months after the revolution say they are certain Mr Ahmadinejad was among those who captured them.
He insists he was not there, and several known hostage-takers - now his strong political opponents - deny he was with them.
His website says he joined the Revolutionary Guards voluntarily after the revolution, and he is also reported to have served in covert operations during the 1980-88 Iran-Iraq war.
Mr Ahmadinejad reportedly spent no money on his first presidential campaign in 2005 - but he was backed by powerful conservatives who used their network of mosques to mobilise support for him.
He also had the support of a group of younger, second-generation revolutionaries known as the Abadgaran, or Developers, who are strong in the Iranian parliament, the Majlis.
The campaign focused on poverty, social justice and the distribution of wealth inside Iran.
He also repeatedly defended his country's nuclear programme, which worried the US and European Union.
Once in power, Mr Ahmadinejad made a defiant speech at the UN on the nuclear issue and refused to back down on Tehran's decision to resume uranium conversion.
He continues to defend what he says is his country's right to civilian nuclear energy and its missile development programme.
In June 2010 when the UN Security Council voted in favour of fresh sanctions against Iran over its nuclear programme, he said they should be thrown in the dustbin like a "used handkerchief".
Iran blames political pressure from the US and its allies for Security Council decisions.
It insists it will not break its obligations under the Nuclear Non-Proliferation Treaty and will not use its technology to make a nuclear bomb.
Mr Ahmadinejad has also angered Western powers with his views on Israel.
He has called for an end to the Israeli state and has described the Holocaust as a myth.
In October 2005, Mr Ahmadinejad made a statement in which he envisaged the replacement of Israel with a Palestinian state.
He was quoting Ayatollah Khomeini, the leader of Iran's Islamic Revolution, and his words were widely translated as a call for Israel to be "wiped off the map", though this translation is disputed.
That was quickly interpreted by Western news agencies as an oblique threat to Israel.
Mr Ahmadinejad has since stated that his speech was exaggerated and misinterpreted.
He denied that he meant military intervention and said instead that Israel's "Zionist regime" would eventually collapse on its own.
During a speech at the UN in April 2009, he commented that Israel was a state founded on racist principles, an outburst that prompted a walk-out by delegates from at least 30 countries but earned him a hero's welcome on his return home.
Mr Ahmadinejad has made some small-scale concessions to moderates since 2005.
He often speaks of women as being at the heart of Iranian society. He talks of empowering them and makes much of his plan to provide insurance for housewives and share Iran's oil wealth with poorer families.
He said he would not be confrontational in enforcing a campaign in Tehran to insist women obeyed Iran's strict Islamic dress codes and has also allowed women into major sporting events for the first time since 1979.
Mr Ahmadinejad maintains a populist streak, calling his personal website Mardomyar, or the People's Friend.
He also has a reputation for living a simple life and has campaigned against corruption.
A populist hardliner, Mahmoud Ahmadinejad has often courted controversy since becoming the sixth president of the Islamic Republic of Iran in 2005.