The history between the Ottoman Empire and Morocco constitutes a strong basis for the current bilateral relations without any historical prejudices.
The brothers Hayreddin and Oruç Barbaroussa became very powerful with the Fall of Tlemcen (1517) and the Capture of Algiers (1516) and again the definitive Capture of Algiers (1529) with the help of the Ottoman Sultan after they joined the empire, they progressively ended the influence of Spain and the Zayyanid with whom they had signed a peace treaties during the rise of the Ottoman influence in the west Mediterranean.
[2] In June 1545, Hasan Pasha, son of Hayreddin Barbarossa and ruler of the Regency of Algiers, occupied the city of Tlemcen, where he set a Turkish garrison, and put pro-Ottoman Sultan Muhammad on the throne.
Things came to a head in 1545, when the Wattasid ruler of northern Morocco Sultan Ahmad was taken prisoner by his southern rivals the sharifian Sadiyans.
After this had occurred his successor Ali Abu Hassun, who was now the Wattasid ruler in northern Morocco, with the hopes of gaining military help in order to remain in power, formally recognised the authority of the Ottomans and declared himself as their vassal.
[2] Mansur bin Ghani went to Spain with Count Alcaudete, the Governor of Oran, to obtain military support for a campaign against the Ottomans.
[2] The Ottomans were unable to intervene when the Saadians conquered Fez in 1549 and ousted pro-Ottoman regent Ali Abu Hassun.
The Saadian ruler Mohammed ash-Sheikh sent an army of 30,000 men, led by his son Muhammad al-Harran, to invade Tlemcen in 1551.
[2] In 1552 Suleiman the Magnificent attempted a diplomatic rapprochement with the Saadians, putting the blame on Hasan Pasha for the conflict, and removing him from his rule in Algiers.
[2] He was replaced by Salah Rais, who nevertheless marched on Fez and captured the city at the beginning of the year in 1554, when the Moroccan ruler Mohammed ash-Sheikh rejected cooperation with the Ottomans.
[2] Hasan Pasha was again named beylerbey of Algiers in June 1557, in order to continue the fight against the Moroccan ruler, who had formed an alliance with the Spanish against the Ottomans.
[7][9] Hasan Pasha invaded Morocco in early 1558, but he was stopped by the Moroccans north of Fez at the Battle of Wadi al-Laban, and had to retreat upon hearing of Spanish preparations for an offensive from Oran.
[7] The failure of the expedition of Mostaganem ended attempts at a grand alliance between Spain and Morocco against the common Ottoman enemy.
[11] Murad III organised an expedition after Abd al-Malik had made a deal with him to make Morocco an Ottoman vassal in exchange for support in gaining the Saadi throne.