Somewhere in time when
God created the universe and planned to create man this happened:
And [mention, O Muhammad], when your Lord said to the angels, "Indeed, I will make upon the earth a successive authority." They said, "Will You place upon it one who causes corruption therein and sheds blood, while we declare Your praise and sanctify You?" Allah said, "Indeed, I know that which you do not know."
(Quran, al-Baqarah 2: 30).
And when God created man
this followed :
(Quran,
al-Baqarah 2: 31-34).
Conversation between God and Satan:
(Allah) said: "O Iblees (Satan)! What is your reason for not being among the prostrators?"(Iblees (Satan)) said: "I am not the one to prostrate myself to a human being, whom You created from sounding clay of altered black smooth mud"
(Quran, al-Hijr: 32 – 33).
"And verily, the curse shall be upon you till the Day of Recompense (i.e. the Day of Resurrection)"
(Quran, al-Hijr: 34 – 35).
(Quran, Al-Aaraf: 16,17).
Learning:
The common learning that everyone concludes from the above story is not to be arrogant. Do what God asks you to do and follow Him in whichever path He asks you to.However, what we don’t understand is that even when Satan was being cursed and exploded, he prayed to God and asked Him to give him a long life and permission to deviate human beings from the righteous path. So, Satan taught us that even when you are full of sins and wickedness, you should not stop praying to God. We should believe that God accepts prayers from all-- even from the sinned and wicked creations like Satan.
"Come, come again,
whoever you are, come!
Heathen, fire worshipper
or idolatrous, come!
Come even if you broke
your penitence a hundred times,
Ours is the portal of hope,
come as you are."
(Rumi)



