Why is there punishment for sins if God gave us free will to do them?

 From an Islamic point of view, we are the blessed creation of Allah whom He created for His worship. He provided us with divine guidance through His prophets and revelations, and He spread innumerable signs of His existence throughout the world for us to ponder upon and comprehend using our intellect and the senses given to us. Though most matters are decreed by Allah almighty, the choice remains in our hands to believe in and worship Allah or to disobey Him.

God favored us over all of His creations by granting us intellect and free will. He provided us with the knowledge of good and bad, and warned us about the consequences of the choices we make and the actions we do – whether they are righteous or sinful – through His divine guidance and by sending prophets. He did not give us free will to punish us, but rather to test us to see whether we are able to recognize the existence of God and follow the path laid out by Him for our success using our intellect. Allah delegated will to mankind to be used in the service of good. Our will is “free will” in the sense that we are not forced to do what we do. We are rewarded or punished in the Hereafter based upon what we did with our God-given will. We will not face punishment if we choose to follow God’s commands and tread our life according to His will as mentioned in the prophetic narration.

Narrated Mu`adh bin Jabal: The Prophet (PBUH) said, "O Mu`adh! Do you know what Allah's Right upon His slaves is?" I said, "Allah and His Apostle know best." The Prophet (PBUH) said, "To worship Him (Allah) Alone and to join none in worship with Him (Allah). Do you know what their right upon Him is?" I replied, "Allah and His Apostle know best." The Prophet (?) said, "Not to punish them (if they do so). Sahih al-Bukhari 7373

The deeds we do, whether they are good or bad, only benefit or harm us – they don’t benefit or harm God in the least. God gave us the free will and intellect to give us the opportunity to become better human beings, if still, one insists on disbelieving and transgression that is their choice, and, as we mentioned earlier, with choice comes accountability – either reward or punishment. Allah Almighty is just and does not do injustice to His servants. It is our own deeds that lead us to punishment or reward.

“Indeed, Allah does not wrong the people at all, but it is the people who are wronging themselves”. Surah Younus, chapter 10, verse 44.                                                                                                                         

God is extremely merciful and forgiving. Even when we make wrong choices and do wrong things, if we turn to Him to ask for His forgiveness in repentance, He forgives us and gives us chance to mend our ways so that we may be saved from His punishment.

And it is He who accepts repentance from his servants and pardons misdeeds, and He knows what you do. Surah Shura, chapter 42, verse 25.

One should be aware of the fact that the mention of punishment as a consequence of evil deeds is a way of discouraging humans from falling into sin and transgression, just as rewards are mentioned to encourage one to obey Allah and do good deeds. 

We did not send you (O prophet,) but to the entire mankind, as a bearer of good news and as a warner, but most people do not know. Surah Saba, chapter 34, verse, 28.

In conclusion, we are given the choice to become better humans and develop the qualities that make us come closer to our Lord and thereby achieve success in this life and the hereafter. Punishing us is in no interest of our all-merciful God who wants us to be saved from His punishment, hence He sent guidance for us. The choices we make will be the deciding factor of our future abode. Allah knows best. May God guide us.