Lifted from Wikipedia:
The earliest well-known Moroccan independent state was the Berber kingdom of Mauretania under king Bocchus I. This Berber Kingdom of Mauretania (current northern Morocco) dates at least to 110 BC.11 The region remained a part of the Roman Empire until 429 AD when invading Vandals overran the area and Roman administrative presence came to an end.
Ruins of Chellah, Rabat
Umayyad Muslims conquered the region in the 7th century, bringing their language, their system of government, and Islam, to which many of the Berbers slowly converted, mostly after the Arab rule receded. In the Islamic era the first Moroccan Muslim state, independent from the Abbasid Empire, was The Kingdom of Nekor, an emirate in the Rif area. It was founded by the Legend of Salih I ibn Mansur in 710 AD, as a client state to Caliphal grant. According to the Medieval Legends Idris I fled to Morocco from the Abbasids’ massacre against his tribe in Iraq and managed to convince the Awraba Berber tribes to break allegiance to the distant Abbasid caliphs in Baghdad. He founded the Idrisid Dynasty in 780 AD. Morocco became later a center of learning and a major power. From the 11th century onwards, a series of powerful Berber121314 dynasties arose. Under the Almoravid dynasty and the Almohad dynasty, Morocco dominated the Maghreb, Muslim-conquered Spain, and the western Mediterranean region. In the 13th century the Merinids gained power over Morocco and strove to replicate the successes of the Almohads. In the 15th century the Reconquista ended Islamic rule in central and southern Iberia (modern day Spain + Portugal) and many Muslims and Jews fled to Morocco. Under the Saadi Dynasty, the first Moroccan dynasty initiated by ethnic Arabs since the Idrisids, the country would consolidate power and fight off Portuguese and Ottoman invaders, as in the battle of Ksar el Kebir. The reign of Ahmad al-Mansur brought new wealth and prestige to the Sultanate, and a massive invasion of the Songhay Empire was initiated.