Al-Ma'mun's general Tahir ibn Husayn, the victor of Battle of Rayy decided to chase the retreating army.
Eventually Abd al-Rahman was obliged to leave and ask for terms due to resentment of the people of Hamadan at the occupation and rapidly depleting supplies.
After defeating the army of al Muhallabi, Tahir reached the gates of Baghdad and at the right time too as his reinforcements arrived under the leadership of Harthama ibn A'yan.
At one point, a general of al-Amin known as al-Samarqandi used boats to transport mangonels on the river Tigris and bombard enemy positions in the suburbs of Baghdad inflicting more damage to the civilian population than to the besiegers.
[1] As things were getting worse and Tahir ibn Husayn pushed into the city, al-Amin sought to negotiate safe passage out.
Tahir ibn Husayn reluctantly agreed on the condition that al-Amin turn over his scepter, seal and other insignia of the caliphal office.
Muhammad ibn Jarir al-Tabari quotes Tahir's letter to the new Caliph al-Ma'mun informing him of al-Amin's capture and execution and the state of peace resulting in Baghdad.