In 327 BC, when Alexander the Great invaded the territory that is now Pakistan, the territory of the Sandal Bar (where Hafizabad is now located) was a populated area. Big cities were located in the district, and many sub-states were organized here in the presidency of native Maharajas and Rajas. In 997 CE, Sultan Mahmud Ghaznavi, took over the Ghaznavid dynasty empire established by his father, Sultan Sebuktegin, In 1005 he conquered the Shahis in Kabul in 1005, and followed it by the conquests of Punjab region. The Delhi Sultanate and later Mughal Empire ruled the region. The Punjab region became predominantly Muslim due to missionary Sufi saints whose dargahs dot the landscape of Punjab region. During the Delhi Sultanate period a big city was situated at the site of the present village of Mehdiabad. But afterwards, this territory became unpopulated and jungle-like because of a shortage of water and Afghan incursions. This situation persisted until Mughal Emperor Akbar's period. Hafizabad was formerly a place of some importance, and is mentioned in the Ain-i-Akbari as headquarters of a Mahal. Hafiz, the founder, was a favourite of emperor Mughal Emperor Akbar.