In 1341, the emir Habiloghlu occupied the city, before it came under the rule of the Eretnid emirate. Hadji Shadgeldi Pasha took Amasya from the Eretnids under Ali Bey, and successfully fended off the claims of Kadi Burhan al-Din, who had supplanted the Eretnids. Shadgeldi was succeeded by his son Ahmed, who managed to retain his autonomy for a while, with Ottoman assistance; but in 1391/92, the mounting pressure forced him to cede the city to the Ottoman sultan Bayezid I, who installed his son, the future Mehmed I, as its governor.