Through both his parents, Khunji belonged to the affluent and infuential ulama (clergical class) of Fars, who were respected and protected by the Turkmen Aq Qoyunlu. In 1487, Khunji left Shiraz for the third time for a Hajj. He met Sultan Ya'qub of Aq Qoyunlu near the Sahand mountain and agreed to write the history of Aq Qoyunlu dynasty. When the Safavid shah Ismail I started the Shi'ification of Iran and the persecution of Sunni Muslims, Khunji fled to Transoxiana, where he lived for the rest of his life under the patronage of Timurids and Shaybanids.