Why Mahmud Ghaznavi left Hanafi madhab and accepted the Madhab of al Shafiee.
Ibn Khilkan mentioned from Mugeeth al Khalaq fe Tarjeeh al Qawl al Haq by Imam al Juwaini.
فأعرض السلطان عن مذهب أبي حنيفة وتمسك بمذهب الشافعي.
Sultan left the Madhab of Abu Hanifa and accepted the Madhab of al Shafiee.
وفيات الأعيان وأنباء أبناء الزمان – ابن خلكان – ج ٥ – الصفحة ١٨٠
What happened was that There were various Hanafi and Shafiee Scholars present in Mahmud Ghaznavi’s court. Sultan used to search about the right opinions. Once Qaffal prayed like Shafiees and then like Hanafis, Sultan accepted the method of Shafiees as he thought its more closer to authentic ahadith.
(Mugeeth al Khalaq fe Tarjeeh al Qawl al Haq pages 57 to 59 and Wafiyat al ayan vol 5 pages 179-180)
Haji Khalifa said
ولما رأى أن مذهب الشافعي أوفق لظواهر الحديث تشفع بعد ان جمع علماء المذهبين كما ذكره ابن خلكان
When he saw that the Madhab of al Shafiee agreed with apparent ahadeeth then he accepted it in the presence of the scholars of both Madahib as mentioned by Ibn Khilkan.
(كشف الظنون – حاجي خليفة – ج ١ – الصفحة ٤٢٦)
Nevertheless, Mahmud Ghaznavi loved people of hadeeth.
Muhammad Qasim Farishta (born 1560 died in 1620) wrote in his historical book on Indian History:
Sultan sent Abul Tayyab Sahl bin Suleiman who was one of the respected scholar of ahlul hadeeth as a messenger to Aylak Khan.
(Tareekh Farishta 1/40)
Taken from Barre Sagheer me Ahlul Hadeeth ki Amad by Historian Muhammad Ishaq Bhatti pages 266-268 with annotation.