According to tradition, the Alaouites entered Morocco at the end of the 13th century when Al Hassan Addakhil, who lived then in the town of Yanbu in the Hejaz, was brought to Morocco by the inhabitants of Tafilalet to be their imām. They were hoping that, as he was a descendant of Muhammad, his presence would help to improve their date palm crops thanks to his barakah "blessing", an Arabic term meaning a sense of divine presence or charisma. His descendants began to increase their power in southern Morocco after the death of the Saʻdī ruler Ahmad al-Mansur (1578–1603). In 1669, the last Saʻdī sultan was overthrown in the conquest of Marrakesh by Mulay al-Rashid (1664–1672). After the victory over the zāwiya of Dila, who controlled northern Morocco, he was able to unite and pacify the country.

The organization of the sultanate developed under Ismail Ibn Sharif (1672–1727), who, against the opposition of local tribes began to create a unified state. Because the Alaouites had difficult relations with many of the country's Berber and Bedouin-Arab tribes, Isma'īl formed a new army of black slaves, the Black Guard. However, the unity of Morocco did not survive his death—in the ensuing power struggles the tribes became a political and military force once again.

Only with Muhammad III (1757–1790) could the kingdom be pacified again and the administration reorganized. A renewed attempt at centralization was abandoned, and the tribes were allowed to preserve their autonomy. Under Abderrahmane (1822–1859) Morocco fell under the influence of the European powers. When Morocco supported the Algerian independence movement of EmirAbd al-Qadir, it was defeated by the French in 1844 at the Battle of Isly and made to abandon its support.

During the reigns of Muhammad IV (1859–1873) and Hassan I (1873–1894), the Alaouites tried to foster trading links, above all with European countries and the United States. The army and administration were also modernised to improve control over the Berber and Bedouin tribes. With the war against Spain (1859–1860) came direct involvement in European affairs. Although the independence of Morocco was guaranteed at the Conference of Madrid (1880), the French gained ever greater influence. German attempts to counter this growing influence led to the First Moroccan Crisis of 1905–1906 and the Second Moroccan Crisis of 1911.

Eventually the Moroccans were forced to recognise the French Protectorate through the Treaty of Fez, signed on December 3, 1912. At the same time the Rif area of northern Morocco fell under to Spanish control. Under the protectorate (1912–1956), infrastructure was invested in heavily in order to link the Atlantic coastal cities to the hinterland, thus unifying Morocco into a single economic region.

The Protectorate regime faced opposition from the tribes. In 1930, the Berbers were placed under the jurisdiction of French courts, marking the beginning of the independence movement. In 1944, the Istiqlāl was founded and was supported by Sultan Muhammad V (1927–1961). France was obliged to grant Morocco independence on March 2, 1956, leaving behind a legacy of urbanization and industrial economy in some cities—on the one hand—and destruction and isolation in the areas that hosted the Berber resistance against France and Spain on the other.

Sultan Mulay al-Rashid bin Sharif, 1st Alaouite Sultan of Morocco, etc., b. at Sijilmasa (Rissani), Tafilalt, 1631, second son of Prince Moulay Ali Cherif of Tafilalt, educ. privately. Proclaimed at Taza, on the death of his elder half-brother as Sultan of Tafilalt, 2 August 1664. Proclaimed as Sultan of Morocco, etc. at Fez, 1666.