This event has been narrated several times by Shaykh Kamaluddin Ahmed in his talks to students of ‘ilm:
“There was a famous sultan by the name of Nizam-ul-mulk. Although he was a political ruler, he had a lot of love for ulama; he had a lot of love for ilm. So he made all these madaris, all these dar-ul-ulooms, called Madaris-e-Nizamiyya, (the Nizamiyya Academies) of Baghdad and of different places.
So once somebody came to him and said O Nizam-ul-mulk, you have made these Dar-ul-ulooms because you think people are coming there to acquire the deen, people are trying to please Allah.
But just go to your Dar-ul-ulooms and see what the situation is with the students. If you go to one student, he says that I am here because my father is a big khatib, he is a big orator. And I also want to come here so that when I graduate, I can also become a great khatib.
If you go to another student, he will tell you that I am here because my father is qazi. He’s a judge, he has a big post, and he has a lot of respect in society. So I also want to graduate so that people will call me qazi, people will call me mufti.
Then go to another person, he will say I am here because my father is Imam in the Jamia Masjid in the city, and everybody gives him presents, and people respect him, people come to him and ask him to make dua. So I am here because I want to follow my father’s footsteps and become Imam.
So Nizam ul Mulki heard these things, and he said no, I’ll check for myself. So he went to his madrassa, the one in Baghdad. And he asked one student, then he asked another student. And he found that the person was right, that every student he asked seemed to have some riya, some show, some ostentation, some ujub (vanity, thinking how great you are instead of thanking Allah swt).
So anyway, Nizam-ul-mulk found that his students were full of these things. So then he made a niyyah in his heart. He said that this is terrible. That my whole plan has backfired, I am going to close this madrassah down. As he was walking away from the madrassah after making this niyyah, he saw one more student sitting in the corner, and he saw that that student was reading. And Allah swt put it into his heart that he thought: let me go and ask this one last student, what does he say?
So he went to that student, and he said, O student, why are you here. The student just looked up, and looked back down, and kept reading. Just ignored him.
Then he asked a second time, why are you here?
The student just looked up, ignored him, and looked back down.
And the third time he said, O student, do you not know who I am, the founder, the principal, the person who made the madrassah. O student, I am Nizam-ul-Mulk; I am asking you why you are here.
So he looked and said yes, I know who you are. And now I realize that you won’t let me be. So let me answer your question. I am only here because I realized that I need to live a life that is pleasing to Allah swt. And I did not have the knowledge, I did not know how to live such a life, so I have come here because the knowledge of how to please Allah is in these books, so that’s why I am here. So if you would be so kind, please don’t disturb me, because I am busy trying to get that knowledge.
So when Nizam-ul-Mulk saw that there was one student who was still so sincere, he changed his niyyah in his heart, and said no, I will keep this madrassah open. And that student later on became known as the famous Imam Ghazali (ru). That was Imam Ghazali. This is a story of his student days, and he later on became one of the most famous professors of the Nizamiyya Academy in Baghdad.”