In the Classical Antiquity era, Morocco experienced waves of invaders included Phoenicians, Carthaginians, Romans, Vandals, and Byzantines, but with the arrival of Islam, Morocco developed independent states that kept powerful invaders at bay.
In 702 the Berbers submitted to the armies of Islam and adopted Islam. The first Moroccan states formed during these years, but many were still ruled by outsiders, some of whom were part of the Umayyad Caliphate that controlled most of northern Africa c.
700 CE. In 1056, a Berber empire arose however, under the Almoravid Dynasty, and for the next five hundred years, Morocco was governed by Berber dynasties: the Almoravids (from 1056), Almohads (from 1174), Marinid (from 1296), and Wattasid (from 1465).
It was during the Almoravid and Almohad dynasties that Morocco controlled much of North Africa, Spain, and Portugal. In 1238, the Almohad lost control of the Muslim portion of Spain and Portugal, known then as al-Andalus. The Marinid dynasty attempted to regain it, but never succeeded.
In the mid-1500s, a powerful state again arose in Morocco, under the leadership of the Sa"adi dynasty that had taken over southern Morocco in the early 1500s. The Sa"adi defeated the Wattasid in 1554, and then succeeded in holding off incursions by both the Portuguese and Ottoman Empires. In 1603 a succession dispute led to a period of unrest that did not end until 1671 with the formation of the Awalite Dynasty, which still governs Morocco to this day.
During the unrest, Portugal had again gained a foothold in Morocco but was again thrown out by the new leaders.
By the mid 1800s, at a time when the influence of Ottoman Empire was in decline, France and Spain began taking a great interest in Morocco. The Algeciras Conference (1906) that followed the First Moroccan Crisis, formalized France"s special interest in the region (opposed by Germany), and the Treaty of Fez (1912) made Morocco a French protectorate.
Spain gained authority over Ifni (to the south) and Tétouan to the north.