Abū al-ʿAbbās Aḥmad ibn Muḥammad ibn Muḥammad al-Mustaʿīn bi-ʾllāh (Arabic: أبو العباس أحمد بن محمد بن محمد; 836 – 17 October 866), better known by his regnal title al-Mustaʿīn (836 – 17 October 866) was the Abbasid caliph from 862 to 866, during the "Anarchy at Samarra".
They were not willing to have al-Mu'tazz or his brothers; so they elected Ahmad ibn Muhammad (أحمد بن محمد), a nephew of al-Mutawakkil, who took the regnal name al-Mustaʿīn bi-ʾllāh (المستعين بالله "he who looks for help to God").
Arab and other troops based in Baghdad, displeased at the choice, attacked the assembly, broke open the prison, and plundered the armory.
When al-Muntasir died, the Turkic officers gathered together and decided to install the dead caliph's cousin al-Musta'in on the throne.
[3] The new caliph was almost immediately faced with a large riot in Samarra in support of the disenfranchised al-Mu'tazz; the rioters were put down by the military but casualties on both sides were heavy.
After a second attempt to overturn the decision made by the Turks, al-Mu'tazz and his brother would have been put to death, but the vizier intervened and saved their lives, for which act of mercy, his property was seized by the Turkic soldiers, and himself banished to Crete.
This was responded to favorably, and beginning on December 8 a representative from Abu Ahmad's camp began meeting Muhammad in private to discuss how to end the conflict.
Toward the end of December, a provisional agreement between the two sides to depose al-Musta'in was reached, and Abu Ahmad sent five ships loaded with foodstuffs and fodder to relieve the shortages in the city.
Out of loyalty to al-Musta'in and fears that Muhammad's surrender could result in the Samarrans plundering the city, they attacked the palace gates and fought against the guards.
In order to calm the protestors, al-Musta'in appeared above the palace gate with Muhammad at his side, and he gave assurances to the crowd that he was still caliph and that he would lead the Friday prayers the next morning.
[9] Muhammad, for his part, strove to convince the people that he was still acting in al-Musta'in's best interests,[10] while at the same time continuing to negotiate with Abu Ahmad regarding the terms of surrender.
On January 7, Muhammad and Abu Ahmad met in person under a canopy outside the Shammasiyah gate, and the two men agreed that the state revenues would be split among the parties, with two thirds going to the Turks and one third going to Muhammad and the Baghdadi army, and al-Musta'in should be held liable for part of the soldiers' pay.
At first al-Musta'in adamantly refused, but when the Turkish officers Wasif and Bugha stated that they had sided with Muhammad, he agreed to step down.
The delegates returned to Baghdad with the signed document on January 24, accompanied by a group of emissaries sent to secure al-Musta'in's allegiance to al-Mu'tazz.
The conditions signed, the Governor of Baghdad received the ministers and courtiers of al-Musta'in, and, having assured them he had done what he had for the best and to stop further bloodshed, sent them to Samarra to pay homage to the new Caliph.