{ وَاتَّبَعُواْ مَا تَتْلُواْ الشَّيَاطِينُ عَلَى مُلْكِ سُلَيْمَانَ وَمَا كَفَرَ سُلَيْمَانُ وَلَـكِنَّ الشَّيْاطِينَ كَفَرُواْ يُعَلِّمُونَ النَّاسَ السِّحْرَ وَمَا أُنزِلَ عَلَى الْمَلَكَيْنِ بِبَابِلَ هَارُوتَ وَمَارُوتَ وَمَا يُعَلِّمَانِ مِنْ أَحَدٍ حَتَّى يَقُولاَ إِنَّمَا نَحْنُ فِتْنَةٌ فَلاَ تَكْفُرْ فَيَتَعَلَّمُونَ مِنْهُمَا مَا يُفَرِّقُونَ بِهِ بَيْنَ الْمَرْءِ وَزَوْجِهِ وَمَا هُم بِضَآرِّينَ بِهِ مِنْ أَحَدٍ إِلاَّ بِإِذْنِ اللّهِ وَيَتَعَلَّمُونَ مَا يَضُرُّهُمْ وَلاَ يَنفَعُهُمْ وَلَقَدْ عَلِمُواْ لَمَنِ اشْتَرَاهُ مَا لَهُ فِي الآخِرَةِ مِنْ خَلاَقٍ وَلَبِئْسَ مَا شَرَوْاْ بِهِ أَنفُسَهُمْ لَوْ كَانُواْ يَعْلَمُونَ } [البقرة: 102]
102- The Jews, who turned away from the Torah and disobeyed the Prophet (peace be upon him), followed the devils who used to lie and say that Solomon was a sorcerer and he subjugated the universe by his magic. He was a noble and good Prophet who used to supplicate Allah and he was not a disbeliever as the Jews or the devils claimed. May they be cursed! (And to Dâwûd (David) We gave Sulaimân (Solomon). How excellent a slave! Verily, he was ever oft-returning in repentance (to Us)!) [Surat Sad: 30]. However, the devils are the ones who disbelieved because they used to teach people sorcery. Sorcery was not revealed to Harut and Marut in Babel but they were there for a wisdom that we cannot know, for Allah may test His Servants with whatever He wants.
When the people who want to learn sorcery came to them, they said to them: “We are a trial and affliction to people, so do not seek sorcery because those who believe in it and in the permissibility of using it will be disbelievers. Yet, the people learned the sorcery which benefited no one, but rather, spoiled the relation and intimacy between a man and his wife. They did not harm anyone but by the will of Allah, Who may remove His Protection from any of His Servants, leaving them to the sorcery of the sorcerers, but if He did not, the sorcerers cannot harm anyone except by the leave of Allah.
Thus, they learned what may harm them in their religion because they intend to do harm to the people. The Jews who replaced faith with sorcery knew that they will have no share in the Hereafter; so how bad is their trade!
Ibn Kathir (may Allah bestow mercy on his soul) said concerning the story of Harut Marut: The output of the story is related to the news of the Children of Israel because it does not contain an authentic Hadith Marfu` (a Hadith narrated from the Prophet with a connected or disconnected chain of narration), and the Prophet (peace be upon him) did not speak out of whims. The outward of the context is to tell the story without details or verbosity because we believe in what was revealed in the Qur’an in the way which Allah intended and Allah knows best the fact of the case.