Maghrebi war (1699–1702)
The Maghrebi war (1699-1702) was a conflict involving a Tunisian, Tripolitanian, and Moroccan coalition, and the Deylik of Algiers. It was an important milestone in the further weakening of the already fragile Ottoman grip over the Maghreb, as both sides utterly ignored the Ottoman sultan's pleas to sign a peace treaty. This war also led to the renewal of the Muradid infighting, which would later lead to the establishment of the Beylik of Tunis, and the Husainid dynasty in 1705.

Background

Ambitions 
As early as the 1690s, a change of course was made in the foreign policy of the Regency of Algiers by the dey Hadj Chaabane whom diverted Algiers from the wars of sea against the Europeans and sought to federate the entire Maghreb under his authority. He specifically was interested in expanding his borders by enlarging the Beylik of Mascara. He also wanted to annex Tunis, and make it a fourth Beylik-governorate.

During this time the freshly rising Alaouite dynasty also sought to expand their control, and especially fancied the Beylik of Mascara, an Algerian governorate in the west of the country.

Algerian-Moroccan conflicts 

During his early reign, Alaouite sultan Ismail Ibn Sharif directed several unsuccessful invasions to take over the Beylik of Mascara which all resulted in defeats in 1672 at Tlemcen, 1678 at the Oued Za, 1692 at the Moulouya, 1693 in Oran and 1694 in Djidiouïa.

The Battle of Moulouya, in 1692 ended in an Algerian victory, and the Algerians pushing the Moroccans back all the way to Fez.

The Siege of Oran was an attempt by the Moroccans to seize Spanish Oran and the Algerian territories around them. The allied Spanish-Algerian armies successfully pushed the Moroccans back.

In the resulting peace treaty the Sultan of Morocco was forced to give up every territory east of the Moulouya River.

Algerian-Tunisian war 

In 1694 Chabane invaded Tunisia with the help of a pretender called Mohammed ben Cheker. The resulting war ended in the total annexation of Tunis for several months. During that time Chaabane appointed ben Cheker as the Bey of Tunis, a new Beylik of Algiers similarly functioning in the same way as the Beylik of Constantine.

This didn't last long though, and in 1695 the Tunisians revolted and defeated ben Cheker at the battle of Kairouan, reinstating the Muradid dynasty. This ended the short-lived Algerian Beylik of Tunis.

Tunisian-Moroccan alliance 
After these defeats the two countries of Tunis and Morocco decided to ally against the Algerians, whom recently experienced instability following a revolt by the Odjak of Algiers against the Corsair-friendly Hadj Chaabane. The two countries decided to prepare for a coordinated invasion of Algiers.

The war

Moroccan invasion of the Beylik of Mascara 

In the Hijri year of 1111 (1699-1700) the Moroccan army, mainly composed of the Black Guard, led by the son of Ismail, Zeidan or Zidan entered Algeria. As the Bey of Mascara, Mustapha Bouchelaghem was at the time not prepared to defend his territory, the invasion was swift. The first engagement was at Tlemcen wherein the Moroccan army besieged the city defended by armed Kouloughlis whom were the main inhabitants of the city. After it fell, the Moroccan army went on and advanced to Mascara, the capital of the Beylik where he looted the palace of the Bey, despite his initial success this campaign resulted in a peace negotiation which infuriated Moulay Ismail and caused him to direct another offensive against the Algerians. These achievements by Moulay Zidan are apparently unknown to contemporary comment and may be assumed to be fiction, however during the same year a Moroccan prince was defeated by an Algerian battalion in September while he was on a minor tax raid in Tlemcen.

Tunisian campaign on Constantine 

In 1699 Tunisian troops reinforced with Tripolitanian ones invaded the Beylik of Constantine, at the same time as the Moroccan ones. The Bey of Constantine at the time, Ali Khodja Bey was more prepared than his Mascaran counterpart, although he failed decisively in a battle near Constantine against Murad III Bey, and his commander Ibrahim Sharif of Tunis. Although his goal was not necessarily clear, he most likely wanted to incorporate Kabylia and Constantinois into Tunisia in a similar fashion to the Hafsid dynasty.

Moroccan retreat 
After several raids against him, Zidan chose to retreat from captured Algerian territories, keeping his loot with him. This retreat angered his father whom promptly replaced him.

Algerian counter-attack

Battle of Jouami' al' Ulama 

On 3 October 1700 Algerian forces successfully defeated the armies of Murad in the Battle of Jouami' al' Ulama near Sétif. This defeat cause a rout, and Murad had to retreat back into Tunisian territories, abandoning all of his gains. Although he did lose, he attempted to raise another army in hopes of attacking Algeria again. He also sent his commander Ibrahim Sharif to Constantinople to recruit additional janissaries.

Battle of Chélif 

Ismail led a large army of up to 50,000 men against the armies of the Dey of Algiers. Despite the numerical inferiority of the Algerians, they were still able to achieve a great victory over the Moroccan armies, killing about 3,000 Moroccans. This defeat led a to a Ceasefire between Algiers and Morocco, and allowed the Dey to focus on Tunis instead.

Assassination of Murad III, end of the war 
In 1702, Murad III was raising an army to start another offensive into Algeria. Ibrahim Sharif returned from Constantinople with a large amount of Turkish janissaries which pleased Murad, although unknown to him, Sharif had specific plans. Acting on secret orders from the Ottoman Sultan, on 2 June he assassinated Murad and killed his entire family, and restored Ottoman control over the territory, and ending the Muradid dynasty. He signed a peace treaty with the Algerians a few weeks later, ending the war with a Status quo ante bellum.

Aftermath 
Although the wars had no short-term effects other than destruction, its long-term effects were more important. In 1705, as a direct result of this war, another war broke out between Algiers and Tunis, which led to the establishment of the Husainid dynasty.

After Hadj Mustapha lost popularity an Algiers as a result of these wars he was killed in 1705, and after his death, the country fell into chaos, as Deys were often killed within days or sometimes hours of being elected. It wasn't until 1710 that Algiers was stabilized by a new Dey called Baba Ali Chaouch.

These, and further military defeats of Tripolitania would lead to instability, and the ascension of the Karamanli dynasty in 1711.

Morocco was the least touched in these conflicts although they had to give up their claims over western Algeria, and for some time Oujda.

References 

Wars involving Algeria
Wars involving Tunisia
Wars involving Morocco
Conflicts in 1699
Conflicts in 1700
Conflicts in 1701
Conflicts in 1702