‘Ali ibn Ibrahim and Ayyashi have in their tafsirs, narrated from Imam Muhammad al-Baqir that every day and every night, angels used to descend for taking up deeds of the middle men (of the residents of the earth). On seeing these deeds, the residents of the heavens began to find fault in the sins being committed by the dwellers of the earth like disobeying Allah and making false allegations against Him. They used to say that Allah is high and above what the dwellers of the world say about Him. At last, a group of angels requested Allah: O Nourisher! Do You not frown upon Your creations who make false allegations against You and who disobey You even though You have prohibited them from doing such things? O Lord! You tolerate them even though all of them are in Your control and are living a life of ease due to Your bounties. So Allah willed to show to the angels what is His perfect Power and how He issues His commandments in the world of His creation, and to make the angels aware of His blessings as they have been created sinless and He has made them distinct among His creations and has made them ever obedient and has not given them the power of sinning. So He said to the angels: Select two beings from your community so that I may send them to the earth and give them the nature of human beings and put in them desire for eating, drinking, etc. and introduce yearnings in their minds like the children of Adam. Then I will take a test of them in the matter of My obedience and worship. The angels selected Harut and Marut from among them as they were foremost in finding faults in humans and in asking for Allah’s chastisement for the humans. Allah told them: Now I have put all those desires and needs in you which I have created in men. Do not include anyone (do not make any partner) in worshipping Me, do not commit adultery and do not drink wine.
Then He removed all curtains of the skies so that He may make His power visible to the angels. He sent those two angels on earth in the form of two handsome men and made them camp in Babylon. When they came to earth, they saw a very beautiful woman advancing towards them. She was fully adorned and perfumed and her face was unveiled. Now the two angels had a thought about the woman that they were prohibited from. They discussed it between themselves and decided to walk away from the temptation. After walking a few steps, passion overpowered them. They returned to that woman and asked her to allow them to have sex with her. That woman said, My religion does not allow me to lie with you until you adopt my religion.
They inquired her religion. She said, I can only fulfill the desire of the one who worships my Allah and who prostrates before my Allah and she pointed towards an idol saying, this is my Allah. The angels looked at one another and exclaimed: Now two sins (instead of one) confront us viz., one of Polytheism and another adultery. Then they consulted again but desire overtook them. So they said to the woman: We are agreeable. Then she said, As you are prepared to bow before the idol you must first drink wine because prostration without taking wine is not recognized. The angels consulted again and said, Now three sins confront us-drinking wine, having illegal sex and prostrating before an idol. They said to that woman: Verily you have proved a big trouble for us. Still we are ready to do whatever you say. So they drank the wine and bowed before the idol and they readied to lie with that woman when suddenly a beggar entered the place. They asked him: Who are you and why have you come here? He replied, “Your condition makes me doubt your intention. You are fearful and frightened and yet you have brought this woman to a solitary place. Surely you are evil fellows.” Saying this he went away. Then that woman told the two, “I swear by my Allah that this man knows you and is aware of your place and has gone to disgrace both you and me. So I will not draw near to you. You must first kill him so that he may not be able to disgrace us and then come back to me and do whatever you wish.” At once, the two ran after that man and killed him. When they returned they did not find the woman there and found that suddenly their clothes had fallen down and they had become naked. They began to bite their fingers with an intense felling of shame and sorrow.
Allah revealed, “I sent you only for a while on earth to live with My creation, but in this little time you committed all the sins from which I had prohibited you from. You did not feel ashamed before Me, though it was you who were more than all others against the dwellers of the earth due to their disobedience and wanted that they should be punished. It was due to this fact that I had given you such a nature which did not entertain any wish to commit sin and it was I who had protected you from disobeying Me. Now when I lifted my protection from you and left you to your own you behaved like this. Now it is up to you to select for yourself either a worldly punishment or the other worldly chastisement.” One of the angels said, “Since we have come to the world we might as well enjoy our desires fully until we get the punishment of the Hereafter.” Another said, “The worldly punishment is limited which will end some day. But the chastisement of the Hereafter is everlasting. It is much harsher which we do not prefer.” So they opted for this world’s punishment and continued to teach magic to the people. They did so for a long time and when they perfected that teaching they were hung upside down in the air and will remain like that until the Day of the Resurrection Day.
Reference: Hayat Al-Qulub, Vol. 1, Stories of the Prophets