In order to show the people, the correct path, to make them aware that Allah is the lord, it was necessary to make them witness a miracle.
Interestingly, the people of Egypt used to practice magic to a great extent, so much so that Pharaoh used magic to control the nation. He had set up an academy where people used to practice magic under his commandments.
It was magic that made him powerful, made people fearful and made them believe that he is the god. Before going further, it is important to mention here that Hazrat Musa A.S. was brought up in the palace of Pharaoh.
Once Hazrat Musa A.S. killed an Egyptian while supporting someone from Bani Israel. The Pharaoh passed the verdict that Musa should be captured. When Hazrat Musa A.S came to know this, he left the city immediately to Madyan.
He lived for 10 years near the place which can be located on Google Maps by the name wells of Moses.
When Prophet Musa A.S. was sent down with the message of Allah towards them, Allah blessed Musa A.S. with the miracle that was magical in nature.
You see, it was important to show them that what they practice is simply magic or illusion, but what Allah Almighty possesses is real powers.
When Musa A.S. went to Pharaoh and invited him towards Islam and showed him the miracle, he ought that it was simply magic.
Pharaoh was so powerful at that time that he could simply kill Hazrat Musa A.S. and nobody could raise a question. But there is a reason Pharaoh did not kill Prophet Musa.
Pharaoh asked Musa A.S. to hold a magical contest with Pharaoh’s magicians. Musa A.S. accepted the challenge and asked that the contest is held on the festival day at forenoon. – Quran 20:59
The arrogant Pharaoh agreed to it and called upon expert magicians from all over Egypt. The Prophet Musa A.S. was there with the message of Allah. He was there to guide them; he was not there to defeat them.
Musa A.S. and Haroon A.S. were on one side while according to traditions, on the other side there were at-least 70 magicians.
Musa A.S. was a bit fearful, not because his Iman was weak but it was something natural. The fear of a believer is temporary, he overcomes it shortly and so did Musa A.S.
Quran reveals Surah Taha (66-69) that Musa A.S. told them to throw their sticks firsts, as they did, it seemed that they were moving quickly.
It was pure magic. Musa A.S. became hesitant but Allah comforted him and asked him to throw his stick. The stick became a snake and ate up what was the result of pure magic (their ropes)! The magicians knew well that what Musa A.S. was able to do was not magic.
No magical trick could give such results. Magicians can only create illusions: they cannot give life to a non-living thing, only Allah Almighty can!
According to Quran (20:70), the magicians fell down on the ground and announced that they believe in Allah, the lord of all, the Lord of Musa A.S. and Haroon A.S.
On this Pharaoh got frightened and fearful. He feared that people won’t give him the status of god anymore. As a result, he ordered punishment for the magicians who had announced that they believed in Allah.
In Quran (7:123 and 7:124) it is stated that Pharaoh threatened them that their opposite leg and hand would be cut off and they would be given traditional death punishment (crucify).
Surah Taha reveals that Pharaoh wanted to frighten them, but they had such a strong belief in Allah now that they did not fear death. They were given the punishment yet they died as believers!