According to reliable chains, Imam as-Sadiq said, “Amir al-Mu’minin said, Daniel was an orphan. Neither his father nor mother were living. An old woman of The Israelites had nourished Daniel. The king of that time had appointed two Qadis (judges). A very virtuous man was a friend of both of them. He had a very beautiful, pious and God-worshipping wife. That pious man used to meet the king also. Facing a problem the king told the two judges that he required a reliable man to whom he could entrust such and such work. The two judges recommended the name of the aforesaid person. The king dispatched that pious man on that job. While going away that pious man asked his two friends to look after his wife during his absence. They went to his house to inquire about her well-being. As she was extraordinarily beautiful both of them fell in love with her at first sight and desired to have an illicit relationship with her. But she did not agree. They threatened her as such “We will give witness before the king that you have committed adultery and he will stone you to death.” But she was adamant and retorted: “Do whatever you can but I will not tolerate such a heinous misdeed.” Both those dishonest men (helplessly) went to the king and gave a false statement that she had committed adultery. The king was shocked to hear it because he had heard a lot about her piety and chastity and was all praise for it.
On the other hand he could not reject the witness given by the two judges. At last after a lot of thinking he told them that he would accept their witness but would give the punishment of stoning her after three days. He announced in the city that there would be a stoning of a Allah-fearing and virtuous woman of the town as she had committed adultery according to the witness given by two judges and called upon the people to gather at a certain place for the stoning. The people were not prepared to agree with this allegation which they knew was false and continued to argue with the king. The king consulted his vizier and asked him whether he could show him a way by which the woman could be saved? He said, “No.”
On the third day when that lady was to be stoned to death the vizier got out of his home and proceeded toward the king’s court. On his way he saw some children playing, among whom was also Daniel. The vizier did not know Daniel. When the vizier approached them Daniel told the children: “Let us stage a play. I will act as the king, this boy will act as the pious lady and those two boys will take the role of the two judges.” They made an earthen stage and Daniel sat on it. He took a sword made of reed in his hand and ordered the rest of the boys to take one of the two witnesses and make him stand afar and to make the other witness stand at a distance on the other side. Then he called one of the witnesses near him and said, “Tell the truth otherwise you will be decapitated instantly.” The vizier was observing this game. The boy witness said that the pious woman committed adultery.
Daniel asked: “When?” He said at such time and on such day. Then Daniel asked: “With whom?” He replied, “With the son of such and such man.” Daniel then asked: “At what place.” He replied, “At such and such place.” Then Daniel called the other witness and asked him: “What is your witnessing?” He said, “I give witness that this lady has committed adultery.” He was asked: “when?” He replied, “At this time.” He asked: “With whom?” He said, “With that person.” He asked: “At what place?” He said, “At such and such place.” As there was contradiction in the statements of the two, Daniel exclaimed: “Allahu Akbar” (Allah is the Greatest). These two have given false witness. Then he ordered that an announcement should be made that the people should gather to witness the execution of the two persons who gave false testimony. Observing this wonderful judgment of Daniel the vizier rushed to the king and described it to him. The king called those two qadis, made them stand apart at an out-of-hearing distance. Then he called one of the two and questioned him about the pious woman as Daniel had done. Then he called the other person and interrogated him likewise. As there was obvious difference between their accounts the king called upon the people to come together and witness the execution of the false witnesses and passed orders for their killing.
Reference: Hayat Al-Qulub, Vol. 1, Stories of the Prophets