This story is mentioned in the beginning of Surah Al-Burooj and it is one of the most wonderful stories that you’ll ever read.
Suhaib reported that Allah’s Messenger (salallahu alaihi wasallam) thus said:
There lived a king before you and he had a (court) magician. As he (the magician) grew old, he said to the king: I have grown old, send some young boy to me so that I should teach him magic. He (the king) sent to him a young man so that he should train him (in magic). And on his way (to the magician) he (the young man) found a monk sitting there.
He (the young man) listened to his (the monk’s) talk and was impressed by it. It became his habit that on his way to the magician he met the monk and set there and he came to the magician (late). He (the magician) beat him because of delay. He made a complaint of that to the monk and he said to him: When you feel afraid of the magician, say: Members of my family had detained me. And when you feel afraid of your family you should say: The magician had detained me.
It so happened that there came a huge beast (of prey) and it blocked the way of the people, and he (the young boy) said: I will come to know today whether the magician is superior or the monk is superior. He picked up a stone and said: O Allah, if the affair of the monk is dearer to Thee than the affair of the magician, cause death to this animal so that the people should be able to move about freely.
He threw that stone towards it and killed it and the people began to move about (on the path freely). He (the young man) then came to that monk and informed him and the monk said: Son, today you are superior to me. Your affair has come to a stage where I find that you would be soon put to a trial, and in case you are put to a trial don’t give my clue.
That young man began to treat the blind and those suffering from leprosy and he in fact began to cure people from (all kinds) of illness. When a companion of the king who had gone blind heard about him, he came to him with numerous gifts and said: If you cure me all these things collected together here would be yours. He said: I myself do not cure anyone. It is Allah Who cures and if you affirm faith in Allah, I shall also supplicate Allah to cure you.
He affirmed his faith in Allah and Allah cured him and he came to the king and sat by his side as he used to sit before. The king said to him: Who restored your eyesight? He said: My Lord. Thereupon he said: It means that your Lord is One besides me. He said: My Lord and your Lord is Allah, so he (the king) took hold of him and tormented him till he gave a clue of that boy. The young man was thus summoned and the king said to him: O boy, it has been conveyed to me that you have become so much proficient in your magic that you cure the blind and those suffering from leprosy and you do such and such things. Thereupon he said: I do not cure anyone; it is Allah Who cures, and he (the king) took hold of him and began to torment him. So he gave a clue of the monk.
The monk was thus summoned and it was said to him: You should turn back from your religion. He, however, refused to do so. He (ordered) for a saw to be brought (and when it was done) he (the king) placed it in the middle of his head and tore it into parts until a part fell down. Then the courtier of the king was brought and it was said to him: Turn back from your religion. And he refused to do so, and the saw was placed in the midst of his head and it was torn until a part fell down.
Then that young boy was brought and it was said to him: Turn back from your religion. He refused to do so and he was handed over to a group of his courtiers. And he said to them: Take him to such and such mountain; make him climb up that mountain and when you reach its top (ask him to renounce his faith) but if he refuses to do so, then throw him (down the mountain).
So they took him and made him climb up the mountain and he said: ‘O Allah, save me from them (in any way) Thou likest’ and the mountain began to quake and they all fell down and that person came walking to the king. The king said to him: ‘What has happened to your companions?’ He said: ‘Allah has saved me from them’. He again handed him to some of his courtiers and said: ‘Take him and carry him in a small boat and when you reach the middle of the ocean (ask him to renounce) his religion, but if he does not renounce his religion throw him (into the water).’
So they took him and he said: ‘O Allah, save me from them and what they want to do.’ It was quite soon that the boat turned over and they were drowned and he came walking to the king, and the king said to him: ‘What has happened to your companions?’ He said: ‘Allah has saved me from them’, and he said to the king: ‘You cannot kill me until you do what I ask you to do. ‘And he said: ‘What is that?’ He said: ‘You should gather people in a plain and hang me by the trunk (of a tree).
Then take hold of an arrow from the quiver and say: In the name of Allah, the Lord of the worlds; then shoot an arrow and if you do that then you would be able to kill me.’ So he (the king) called the people in an open plain and tied him (the boy) to the trunk of a tree, then he took hold of an arrow from his quiver and then placed the arrow in the bow and then said: In the name of Allah, the Lord of the young boy; he then shot an arrow and it bit his temple.
He (the boy) placed his hands upon the temple where the arrow had bit him and he died and the people said: We affirm our faith in the Lord of this young man, we affirm our faith in the Lord of this young man, we affirm our faith in the Lord of this young man. The courtiers came to the king and it was said to him: Do you see that Allah has actually done what you aimed at averting. They (the people) have affirmed their faith in the Lord.
He (the king) commanded ditches to be dug at important points in the path. When these ditches were dug, and the fire was lit in them it was said (to the people): He who would not turn back from his (boy’s) religion would be thrown in the fire or it would be said to them to jump in that. (The people courted death but did not renounce religion) until a woman came with her child and she felt hesitant in jumping into the fire and the child said to her: 0 mother, endure (this ordeal) for it is the truth.”
The Story in the Quran:
By the heaven, holding the big stars.
And by the Promised Day (i.e. the Day of Resurrection);
Cursed were the people of the ditch (the story of the Boy and the King).
Fire supplied (abundantly) with fuel,
When they sat by it (fire),
And they witnessed what they were doing against the believers (i.e. burning them).
They had nothing against them, except that they believed in Allah, the All-Mighty, Worthy of all Praise!
Who, to Whom belongs the dominion of the heavens and the earth! And Allah is Witness over everything.
Verily, those who put into trial the believing men and believing women (by torturing them and burning them), and then do not turn in repentance, (to Allah), will have the torment of Hell, and they will have the punishment of the burning Fire.
Indeed, those who have believed and done righteous deeds will have gardens beneath which rivers flow. That is the great attainment.
(Surah Al-Buruj 85:1-11)