The history of LGBTQ people in Iran spans thousands of years. In pre-Islamic Iran, a tradition of homosexuality existed, however in Zoroastrianism it was condemned

The book Foucault and the Iranian Revolution: Gender and the Seductions of Islam by Janet Afary and Kevin Anderson, features a notable chapter on same-sex relations in Iran. In this chapter, Afary argues that the current regime in the Islamic Republic of Iran is suppressing a long-standing tradition of homosexual culture that dates back over a thousand years. She points out that classical Persian literature, including the works of poets like Attar, Rumi, Saadi, Hafez, Jami, and even Iraj Mirza in the 20th century, is filled with references to homoeroticism and openly discusses beautiful young boys and the practice of pederasty (not to be confused with homosexuality between adult men). Many of the famous love stories celebrated by these poets were between kings and their male servants or slaves. Sometimes, the beloved was the possession of a more powerful individual. Outside of royal courts, homosexuality and homoerotic expressions were accepted in various public settings, including monasteries, seminaries, taverns, military camps, bathhouses, and coffee houses. During the early Safavid era, male houses of prostitution were legally recognized and even paid taxes.

Since the Iranian revolution in 1979, the punishment for homosexuality has been based on Sharia law, with the maximum penalty being death. Transgender people had never been officially addressed by the government leading up to the 1979 revolution, but, after the Islamic Revolution sex reassignment surgery has been allowed through Islamic Law. The government provides up to half the cost of the procedure for those needing financial assistance, upon the provision of necessary documents and supporting proof of an identity disorder.

Pre-Islamic period Ancient Iranian society had a tradition of polytheism and pederasty, which came into sharp conflict during the Achaemenid era. Iranian pederasty and its origins were debated even in ancient times – for example, Herodotus claimed they had learned it from the Greeks: "From the Greeks they have learned to lie with boys."David Grene, Herodotus, p.156, 1987 However, Plutarch asserts that the Iranians used eunuch boys to that end long before contact between the cultures. Gebundene Ausgabe, Plutarch: Malice of Herodotos (Aris & Phillips Classical Texts), 1992, p. 31. In either case, Plato claimed they saw fit to forbid it to the inhabitants of the lands they occupied, since "It does not suit the rulers that their subjects should think noble thoughts, nor that they should form the strong friendships and attachments which these activities, and in particular love, tend to produce."Tom Griffith (Translator), Symposium of Plato, 1993, p112

Sextus Empiricus writing in his "Outlines of Scepticism" (circa C.E 200) asserted that the laws of the Persians were tolerant of homosexual behavior, and the men "indulge in intercourse with males" (1:152)Julia Annas (ed), Sextus Empiricus: Outlines of Scepticism, University of Cambridge Press, 2000, p76

Around 250 BCE, during the Parthian Empire, the Zoroastrian text, the Vendidad, was written. It contains provisions that are part of sexual code promoting procreative sexuality that is interpreted to prohibit same-sex intercourse as a form of demon worship, and thus sinful. Ancient commentary on this passage suggests that those engaging in sodomy could be killed without permission from the Dastur. However, a strong homosexual tradition in Iran is attested to by Greek historians from the 5th century onward, and so the prohibition apparently had little effect on Iranian attitudes or sexual behavior outside the ranks of devout Zoroastrians in rural eastern Iran.

Islamic period

There is a significant amount of Persian literature that explicitly illustrates the ancient existence of homosexuality among Iranians. In Persian poetry, references to sexual love can be found in addition to those of spiritual/religious love. More ghazals (love poems) and texts in Saadi's Bustan and Gulistan portray love between males than between male and female. In some poems, Sa'di's beloved is a young man, not a beautiful woman.

Safavid era European travelers remarked on the taste that Shah Abbas of Iran (1588–1629) had for wine and festivities, but also for charming pages and cup bearers. A painting by Riza Abbasi with homo-erotic qualities shows the ruler enjoying such delights.Welch A., "Worldly and Otherworldly Love in Safavi Painting", Persian Painting from the Mongols to the Qajars, Éditions R. Hillenbrand, Londres, 2000, p. 303 et p. 309.

Pre-modern homosexuality Throughout his study, historian Khaled El-Rouayheb has argued that same sex relations during the Islamic period contrasts from what is considered modern day homosexuality. His argument supports the notion of homosexuality as a concept that was only recently established as an identity.

The act of penetration between two men was previously regarded as an act of dominance rather than a sexual act - with one person presiding dominance over the other. The individual dominating the other person would be considered active and the other passive. Sexual acts involving individuals of the same sex were understood through the concept of passive and active participants rather than by gender. The man that was considered the passive participant was deplored due to it being seen as the more feminine role in comparison to the active participant who would be seen as more masculine and dominating.

A shift in the view of same-sex relation emerged during the nineteenth century. European influence created a separate narrative of what was considered homosexuality which resulted in Iran rejecting the idea of homosexuality altogether and embracing the concept of homosociality. Even still, this concept that men were participating in sexual practices with other men was still prevalent and seen as an act rather than a characteristic.

20th century Iran

Qajar era In 1914, Magnus Hirschfeld wrote that "sodomy, tribadism" was punishable with capital punishment under the Sublime State of Iran under "Shiite religious laws", however in the case of women, this only applied with the fourth convictions, the other previous three convictions received 100 lashes. Although he also stated that "In recent years, the religious penal code has been implemented very negligently in practice. No one at the German embassy has heard about a conviction as a result of the crime in question."Hirschfeld, Magnus. Die Homosexualität des Mannes und des Weibes. Berlin: L. Marcus, 1914. Print.

Pahlavi Era Under the rule of Mohammad Reza Pahlavi, the last monarch of the Pahlavi dynasty, homosexuality was tolerated, even to the point of allowing news coverage of a same-sex wedding. In the late 1970s, some Iranians even began to talk about starting up a gay rights organization, similar to the Gay Liberation movement. Until the revolution, there were some night clubs in which gay behavior was tolerated. During the Shah's time, however, homosexuality was still taboo everywhere, and often one could not turn to family or friends for support and guidance. There were no public agencies to assist youth or people who were confused or questioning their sexuality.

Due to the fact “Classical Persian literature—like the poems of Attar (died 1220), Rumi (d. 1273), Sa’di (d. 1291), Hafez (d. 1389), Jami (d. 1492), and even 20th century figures such as Iraj Mirza (d. 1926)—were replete with homoerotic allusions and pederasty. Ahmad Kasravi opposed homosexuality and initiated a movement against it, criticizing classical Persian poets like Khayyam, Rumi, Saadi, and Hafez, and advocating for the removal of these poets' works from textbooks. However, during the Pahlavi dynasty, Janet Afary mentions in an interview that literature professors have been compelled to claim that these incredibly beautiful love poems, which clearly express same-sex affection, are not actually about homosexuality. Instead, they are taught as metaphors for love between men and women, despite the explicit references to same-sex relationships.

Ernest Perron, a Swiss national and one of Mohammad Reza Pahlavi's closest friend for 20 years, was homosexual. Reza Shah, who desired to raise his crown prince in a masculine manner and was very watchful of his behavior, became angry at his return to Iran accompanied by a homosexual.

Among the architects and designers who assisted the Shahbanu in decorating the palace, Bijan Safari and Keyvan Khosravani were openly homosexual, yet they were constant companions of the royal family, indicating the level of sensitivity of the Shah and his wife, Farah Diba.

Janet Afary has argued that the 1979 revolution was partly motivated by moral outrage against the Shah's regime, and in particular against a mock same-sex wedding between two young men with ties to the court. She says that this explains the virulence of the anti-homosexual oppression in Iran.

Reza Pahlavi, Crown Prince and the son of Shah Mohammed Reza, argued that LGBT individuals had freedom before the “Mullah Regime” (the 1979 revolution).

Islamic Republic of Iran

After the creation of the Islamic Republic of Iran, thousands of people were executed in public, including homosexuals. On 12 September 1979, Oriana Fallaci, Italian journalist, interviewed Ayatollah Ruhollah Khomeini. She asked him if it was right to shoot homosexuals. He responded that some societies "where men are permitted to give themselves to satisfy other men's desires", and that "the society that we want to build does not permit such things." When she responded about the "boy they shot yesterday, for sodomy," he responded "Corruption, corruption. We have to eliminate corruption." A 1987 report of the United Nations Commission on Human Rights estimated that as many as 7,000 people were shot, hanged, stoned or burned to death after the 1979 revolution.

The new religious government that came to be established after the 1979 Iranian Revolution classed transsexuals and transvestites with gays and lesbians, who were condemned by Islam and faced the punishment of lashing and death under Iran's penal code. In 1986, transsexuals were re-classified as being "heterosexual".

Since the 1979 Iranian revolution, the legal code has been based on Islamic Shari'a law. All sexual relations that occur outside a traditional, heterosexual marriage (i.e. sodomy or adultery) are illegal and no legal distinction is made between consensual or non-consensual sodomy. Homosexual relations that occur between consenting adults in private are a crime and carry a maximum punishment of death. Forced homosexual relations (rape) often results in execution. The death penalty is legal for those above 18, and if a murder was committed, legal at the age of 15. (see Mahmoud Asgari and Ayaz Marhoni whose ages were raised to 19 in court transcripts). Approved by the Iranian Parliament on 30 July 1991, and finally ratified by the Guardian Council on 28 November 1991, articles 108 through 140 distinctly talk about homosexuality and its punishments in detail.

On 11 August 2005, a series of coordinated protests took place across France, Ireland, the United Kingdom, and other locations in response to the hangings of Ayaz Marhoni, 18, and Mahmoud Asgari, who was reported to be either 16 or 17 years old.

Amid the controversy surrounding official claims that the executed youths had sexually assaulted a 13-year-old boy, Afdhere Jama, editor of Huriyah, a digital magazine for Queer Muslims, stated that his contacts in Iran confirmed that the two young men hanged in Mashhad were lovers.