Imam Khomeini in his famous work Forty Hadith explained the matter as following:
If any time any idea of violating Divine commands occurs to him, he should know that this idea has been instilled into his mind by the Satan and his allies, who want to deter him from his good resolutions.