Berber farmers worked this plain long before there was a city. Around 1070 the Almoravids, a Berber reform movement whose empire would stretch from Senegal into Spain, made it their capital. The square in front of their palace became Jemaa el-Fnaa, and the markets that grew between it and the main mosque are where the souks still are. In the 1120s Ali ibn Yusuf ringed the town with walls and dug khettaras, underground channels that carried water from the Atlas foothills.
In 1147 the Almohads, a reform movement of Masmuda tribesmen led by the mahdi Ibn Tumart, took the city after a siege of several months and purged the Almoravid population over three days. Abd al-Mu'min founded the Koutoubia that same year and the Menara Gardens in 1157; Yaqub al-Mansur added the Kasbah, the royal quarter you enter through Bab Agnaou. Scholars from Al-Andalus, among them the philosopher Averroes, came to work here.
The Marinids conquered Marrakech in 1269 and moved the capital to Fez, and the city went into relative decline. It came back in the 16th century under the Saadians, who made it their capital again. Ahmad al-Mansur began El Badi Palace in 1578 with Italian marble, to impress ambassadors from Spain, England and the Ottoman court; today it is a shell of ruined courts with views to the Atlas from its terrace. The Saadians also created the Mellah, the Jewish quarter beside the Kasbah, in 1558 and built the Ben Youssef Madrasa.
In the late 17th century Sultan Moulay Ismail had a pilgrimage created around seven Sufi saints buried in the city, and pilgrims still visit their tombs in a fixed order. Sidi Bel Abbas is the patron saint. Until 1867, European Christians needed the sultan's permission even to enter. Persian and Urdu still call all of Morocco "Marrakesh".
French troops took the city in September 1912, helped by the Glaoui clan of the High Atlas, and T'hami El Glaoui ruled it as pasha for almost the whole Protectorate. He reportedly grew richer than the sultan by taking a cut of nearly every business in town. The planner Henri Prost laid out a new European town at Gueliz from 1914, then the villa quarter of Hivernage in 1928, and La Mamounia hotel opened in 1929. Winston Churchill first visited in 1935, stayed there and painted in its gardens.
Independence came in 1956, and Glaoui's estates were seized by the state. In the 1960s Marrakech turned into a hippie haunt; the Rolling Stones and the Beatles came, and Yves Saint Laurent bought and restored the Majorelle Garden. UNESCO listed the medina in 1985. The medina has changed since: in 1984 about half the city's people lived inside the walls, but by 2004 only 22% did, as houses became commercial properties.
The city has taken hard knocks this century. A bomb in Jemaa el-Fnaa killed 15 people in April 2011 and destroyed the Argana Cafe, and an earthquake struck in September 2023. A month later Marrakech still hosted the annual meetings of the IMF and World Bank, as it had hosted the signing of the agreement that created the World Trade Organization in 1994.