Ubaydallah's rule over Egypt was initially contested by the Abbasid central government, which was still attempting to recover from the loss of authority it had incurred due to the civil war between the rival caliphs al-Amin and al-Ma'mun in 811–813, and shortly after his accession he was forced to deal with an invasion by Khalid ibn Yazid al-Shaybani, who had been delegated by al-Ma'mun to seize command of the country. Ubaydallah decided to offer resistance, and even with Ibn al-Jarawi providing assistance to Khalid he was able to win a battle against the latter on the outskirts of Fustat. Over the next few months the two sides fought several engagements; at length Ibn al-Jarawi withdrew his support and Khalid's position became seriously degraded, and by early 823 he was finally defeated and captured by Ubaydallah. His hold over Fustat now secure, Ubaydallah returned to the capital and afterwards released Khalid, allowing him to withdraw from the province to Mecca.