Marrakech served as the capital of Morocco during three main historical periods: under the Almoravid dynasty (11th-12th centuries), the Almohad Caliphate (12th-13th centuries), and the Saadian dynasty (16th-17th centuries). It also functioned as a capital city during various other times, notably under parts of the early Alawite dynasty, though Fes and Rabat eventually became the more established capitals.