During the 1960s and 1970s, Khatami became involved in the activities of the Militant Clerics' Association, which rallied opposition to the Shah. Through political pamphleteering he supported the efforts of Ayatollah Ruhollah Khomeini, who was then living in exile. He developed a close friendship with Khomeini's son, Ahmad, who would later provide helpful support in resolving post-revolutionary factional disputes.
In the late 1970s, Khatami came to the attention of Ayatollah Mohammad Beheshti, chief ideologue of the Islamic Republican Party and a key tactician of the revolution. Beheshti appointed him to run the Islamic Center of Hamburg (Germany) to help mobilize expatriate Iranians against the Pahlavi regime.
After the revolution he returned to Iran and took over Kayhan Institute, the largest publishing house in Iran. In 1980 he was elected to the first Majlis and in 1982 became minister of culture and Islamic guidance, with responsibility for overseeing Iranian films, publishing, and mass media. Khatami held this position for more than a decade, and his policies heralded a new era of cultural development in post-revolutionary Iran. He became known as a harbinger of tolerance by easing censorship regulations and promoting the publication of many independent journals and book titles. His policies encouraged creativity in the areas of classical music, film, and fiction. The Iranian film industry flourished, receiving much critical acclaim at international festivals over the past two decades. In 1992, Khatami was forced to resign under pressure from conservative clerics who considered his policies too permissive. Between 1992 and 1997 he served as a cultural adviser to President Rafsanjani while heading Iran's national library.
Khatami announced his decision to run for the presidency in January 1997 after consultation with government leaders. In order to qualify, all presidential candidates were screened for ideological purity by the Council of Guardians. This group rejected all but four of 238 presidential hopefuls. Khatami was favored by both radical and pragmatic factions of revolutionary clerics since they wanted to contain the power of conservatives, who at that time enjoyed a majority within the Majlis. While both factions have different views on economic policy, they agreed with Khatami on the need for a more open society. Many Iranians feared that the conservative religious establishment would resort to fraud in order to prevent Khatami from winning the election, but popular support for Khatami was so overwhelming that his opponents had no choice but to let the election proceed without interference. The result was a landslide victory for Khatami on 23 May 1997 in Iran's first freely contested presidential election since 1980. With an estimated 94% of eligible voters casting ballots, Khatami received 69% of the total vote. Despite some disputes with the ruling Council of Guardians over Khatami's attempts at reforms, he was reelected to his second term as president on 8 June 2001, winning 77% of the vote in a field of ten candidates; his nearest rival, a conservative cleric, received only 16% of the vote.