On 31 July 1987, during the Hajj (Arabic for pilgrimage) in Mecca, a clash between Shia pilgrim demonstrators and the Saudi Arabian security forces resulted in the death of more than 400 people. The event has been variously described as a "riot" or a "massacre". It developed from increasing tensions between Shia Iran and Sunni Saudi Arabia since the 1979 Iranian Revolution. Since 1981, Iranian pilgrims have held a political demonstration against Israel and the United States every year at Hajj, but in 1987, a cordon of Saudi police and the Saudi Arabian National Guard sealed part of the planned demonstration route, resulting in a confrontation between them and the pilgrims. This escalated into a violent clash, followed by a deadly stampede.

How many pilgrims died and how they died are both disputed. Both Iran and Saudi Arabia blame each other for the loss of life. Estimates of fatalities range from 400 with thousands more injured (Iranian government); 402, of which 275 were Iranian pilgrims, 85 Saudi police, and 42 pilgrims from other countries (Saudi government); (i.e. the House of Saud).

Additionally, the incident occurred during the last phase of the Iran–Iraq War (1980–1988). Although, at least from the Iraqi side, the conflict was not primarily religious, Saddam Hussein's government did play into concerns by Sunni rulers in the region who relied on religious authority for their position in order to gain their support. This provided fertile ground for sectarian tensions between Saudi Arabia (who started supporting Iraq) and Iran leading up to the events in 1987.

Serious loss of life at Hajj has occurred periodically (in 1990, 1994, 1998, 2001, 2004, 2006, 2015); not surprising in a situation where two million or more pilgrims "all trying to do the same thing in the same place on the same day" while speaking different languages, and are vulnerable to death from suffocation or being physically crushed in the press of the crowd. In 2015, an estimated 2,400 pilgrims, including more than 400 Iranians, were killed in a stampede, that led to a "war of words" between Iran and Saudi Arabia that reached a "fever pitch".

Post-Iranian revolutionary hajjs

For years, Iranian pilgrims had tried to stage demonstrations which are known as "Distancing Ourselves from Mushrikīn" (برائت از مشرکين) in the Muslim holy city of Mecca during the hajj. "Anti shah, anti-Israel and anti-American propaganda during the Hajj" by devotees of Khomeini had been happening since about 1971. After the revolution, following the principles elaborated by Khomeini, they appealed directly to the Muslim pilgrims of other lands gradually heightening political activity in each pilgrimage. In 1981, this was escalated to chanting political slogans in the Masjid al-Haram and the Prophet's Mosque, two of the holiest sites in Islam, resulting in violent clashes with Saudi security and one death. In the same year, King Khalid of Saudi Arabia wrote a letter to Saddam Hussein saying "crush these stupid Iranians" as Saddam continued with the invasion of Iranian territory.

In 1982, Khomeini appointed Hojatoleslam Mohammad Mousavi Khoeiniha as the supervisor and personal representative of Ayatollah Khomeini. Khoeiniha was the "mentor" of the students who had seized the United States Embassy in the Iran hostage crisis. During the hajj that year, Saudi police clashed with demonstrators who Khoiniha addressed in both Medina and Mecca. In Mecca he was arrested, and after giving a speech in Medina following the pilgrimage he was declared an “instigator” and expelled from Saudi Arabia.

During the next few years, both sides tried to calm the situation: Khomeini urged his devotees to maintain peace and order, not to distribute printed political material, and not to criticize Muslim governments. In return, Saudi officials changed their earlier policy and allowed two separate demonstrations to occur: One in Mecca, and the other in Medina. By 1986, the situation was calm enough for Saudi officials to re-open the al-Baqi' cemetery for Shiite pilgrims, and in response, Khomeini's representative formally thanked the Saudi King for the gesture. However, in the same year, Iranian radical Mehdi Hashemi was accused of smuggling explosives on an airplane headed for Saudi Arabia, renewing Saudi fears.

Internationally, U.S. naval forces were being introduced into the Persian Gulf during this time in response to the Iran–Iraq War and heightening political tensions. Then a representative of Khomeini would explain Iran’s case regarding the Iran–Iraq War and an Iranian pilgrimage representative would conduct a referendum among all the pilgrims over the decision of the emir of Kuwait to invite foreign escorts for Kuwaiti tankers.

Even though Mehdi Karrubi, who was Khomeini's official pilgrimage representative that year, tried to assure Saudi officials that the demonstrations would occur in the usual manner and in the agreed routes, it did little to quell the Saudi fears. Saudis and Iranians worked together to go over "the route of the planned demonstration". and

Saudi authorities "remained deeply suspicious".

1987 demonstrations

On Friday 31 July 1987, the demonstration started amid heightened security after Friday's midday prayers, while Iranian pilgrims chanted "Death to America! Death to the Soviet Union! Death to Israel!".

Saudi security personnel deny firing at the demonstrators, Saudi officials insist that no shots were fired, and that all deaths were caused by the melee and stampede. In a Washington news conference, the Saudi ambassador Prince Bandar bin Sultan claimed that "not one bullet was fired", blaming the violence on the Iranian pilgrims who he accused of "brandishing knives, clubs and broken glass drawn from beneath their cloaks". Ami Ayalon, an Israeli politician, wrote that "most of the Iranian pilgrims apparently shot by Saudi security authorities during the demonstration". Martin Kramer also writes that "according to American intelligence sources, the tide was finally turned by reinforcements from the National Guard, who fired tear gas shells into the crowd and then opened fire with pistols and automatic weapons". The Iranian news agency announced that "200 Iranians had been killed and more than 2,000 wounded".

Another high level Iranian leader, Ayatollah Hussein Ali Montazeri, urged Muslim religious leaders to "wrest control of Islam's holy sites in Saudi Arabia from the royal family". On the same day, the Iranian leader Khomeini called on Saudis to overthrow the House of Saud to avenge the pilgrims' deaths.

For three years after the incident—from 1988 to 1990—the Iranian government banned Iranians from going on the Hajj but "diplomatic ties were restored in 1991". Following the incident, Saudi Arabia ended its diplomatic relations with Iran and it reduced the number of permitted Iranian pilgrims to 45,000, down from 150,000 in earlier years. In 1991, Iran and Saudi Arabia renewed diplomatic relations after agreeing to allow Iranian pilgrims to perform the Hajj once more. The total number of pilgrims was set at 115,000, and the demonstrations by Iranians were again allowed, but only in one specific location granted by the Saudis. By this agreement, Iranian pilgrims continued their annual demonstration during the 1990s and 2000s with few or no incidents. They limited their rally to within the confines of their compound in Mecca.

Approximately 20,000 Pakistani troops who were stationed in Saudi Arabia were sent back to Pakistan, because Saudi Arabia was uncomfortable with the presence of Shi'ite soldiers.

Martin Kramer writes that "as no independent investigation will ever be conducted, important details will remain in doubt. But no evidence has been produced by Saudi Arabia or Iran to establish that the other side acted deliberately or with premeditation in order to provoke violence."