As an organiser for the “Organisation of Student Unity”, Ahmadinejad played a key role in the Islamist counter-revolution of 1979-80, establishing control of the universities by purging left-wing and dissident lecturers and students, many of whom were later executed. Following a period as an engineer and military commander in the Iran-Iraq war, he worked in “internal security”, earning notoriety as an interrogator and torturer.
Ahmadinejad was elected mayor of Tehran in May 2003, after a widespread boycott of the city council election brought the ultra-conservative “Alliance of Builders of Islamic Iran” to power on a 12% turnout. In office he pioneered the mixture of Islamist, nationalist and populist measures that has since characterised his presidency. He reversed many of the liberalising reforms of the previous period, carried out Islamist demands such as separate entrances and elevators for men and women in municipal offices, and argued for the bodies of “martyrs” from the Iran-Iraq war to be buried in the city's squares, while also overseeing the distribution of free soup to the poor.
He quarrelled with the “reformist” then-president Mohammad Khatami, who barred him from attending meetings of Iran's Board of Ministers, as is usual for the mayor of Tehran.
Between 1998 and 2001, the reformist wing of Iran’s Islamist elite occupied almost every elected position within the Iranian state, including the presidency. But in the 2005 election, the reformist candidate Mostafa Moeen came fifth. The result was the most viciously right-wing president since the beginning of the Islamic Republic, and the first military leader — as opposed to cleric — to hold the office. Ahmadinejad’s election thus represented a fairly sharp change of direction, an important shift of power within the Iranian ruling class.