Answering the question, Sheikh Ahmad Kutty, A Senior Lecturer and an Islamic Scholar at the Islamic Institute of Toronto, Ontario, Canada , states,
Islam teaches us that Allah created humans with the innate knowledge of what is good and bad, and provided them with the ability to choose what is good and to shun what is evil. He also sent down messengers reinforcing what is in their innate nature. Allah will reward or punish us based on the choices we make.
While the above is to be believed and acted upon, we must also know that since Allah is the Creator and All-Knowing, He knows fully who among His servants are going to choose what is right and who shall choose what is wrong. Allah has foreknowledge of all things and nothing happens in this universe without His Will.
It is part of Allah’s Overriding Will that Humans must choose. So, there is no contradiction as long as we make a distinction between two wills: One is the cosmic will of Allah and the other is prescriptive will; each one of them belongs to a different level.
May Allah guide all of us to what pleases Him!