According to historians of the medieval era including the famous ibn Khaldoun, Morocco would be the first country in the Western sphere where a black powder cannon was used during a military siege. The events would have taken place during the siege of Sijilmassa by the Merinids in 1274, a painting was even painted by a French artist to illustrate the event
On July 20, 1279, during the night, a Marinid commando crossed the Strait of Gibraltar and infiltrated the naval base of Algeciras. The Spanish soldiers there were killed and the ships burned. In a few hours, Castile lost its entire military fleet.
We often hear about the heroic battles of Sagrajas (1086) & Alarcos (1195) won by the Almoravids and Almohads but we hear much less about the resounding successes of the Merinids in Andalusia...
After the fall of the Almohads , Al Andalus once again fell into a period of instability. This fall led, from 1226, to a new period of domination of the taifas, small independent Muslim kingdoms. Among these Taifas, only the Taifa King of Arjona manages to resist the reconquest of the Christian kingdoms He negotiates with them and creates the Nasrid Kingdom of Granada. Meanwhile, in Morocco, the Merinids succeed the Almohads. In 1275 the Nasrid was plagued by civil wars, and the Christian kingdom increased their raids.
Faced with this desperate situation, the King of Granada has no choice but to call for help from the Merinids This was obviously not the first time that the Moroccans crossed the strait to save Al Andalus The Almoravids & Almohads having done it several times before. The Merinid King Yussuf Yacoub Abd Al Haqq accepted and landed his troops on the peninsula in 1275. Nuño Gonzales de Lara, the commander of the Castilian army, therefore prepared his troops to face the arrival of the Moroccans. The Battle of Ecija ended in disaster for the Christian troops. 18,000 Castilians are killed. The Archbishop of Toledo tries to get his revenge which leads to another defeat for the Christians in a timeframe of 2 months, losing the battle of Martos. The two cities have a military garrison, and the rest of the troops return to Morocco.
After these victories, the Merinid troops took control of Al-Jazira Al-Khadra (Algeciras) and Jabal-Tariq (Gibraltar). However, the Merinids had to return immediately to the peninsula after King Alfonso laid siege on the Bay of Algeciras with 24 ships and 80 galleys. They also sent 30,000 knights to surround the city For more than a year, the Castilians besieged the city without success. 1 year after the start of the Siege, the Marinid Sultan learned that the Castilians themselves were lacking food, and that their supplies from Seville were insufficient. The sultan then decides to form a counter-attack on King Alfonso ordering his commando units to surprise the Castilian navy, anchored at isla verde. This attack, which took place during the night, was an absolute surprise. The boats were barely guarded, and in a few hours, Alphonse's navy was totally destroyed. This new defeat forces Alphonse X to sign a peace treaty with the Merinids. The latter will maintain control over their cities on the Iberian Peninsula for several decades.
The history of the Merinids is often very understated in Moroccan history. The golden age of the Merinids will take them as far as Tunisia via Algiers, and Tlemcen. They were known for their unique architecture, and they made Fez the capital of knowledge, a medieval metropolis, and an architectural pearl.
Some information was taken from @Jafkech on Twitter.