Regarding this issue we would like to quote Sheikh Ahmad Kutty , who stated:

Evil thoughts could very well be whisperings of Satan. Whenever such thoughts occur to you, you must turn your attention immediately away from them by seeking refuge in Allah (by saying: “A`udhu billahi mina ash-shaytani-r-rajim; I seek refuge and protection in Allah from Satan, the accursed”). The Prophet (peace and blessings be upon him) said, “Allah has forgiven my Ummah of the whisperings of their souls so long as they do not talk about it or act accordingly.” (Sahihul Bukhari

To explain this further, our thoughts can be divided into different categories: 1) the constant self-talk or idle thoughts that assail our minds over which we have no control; 2) thoughts that we dwell on; 3) intentions that we formulate based on those thoughts. We are not accountable for the first stage, namely the self-talk unless we dwell on them and nurture them in our minds, since we have no control over them. We are accountable if we dwell on them as well as for the deliberate intentions that we formulate based on these thoughts.

Having said this, it is also crucial for us to consider the treacherous nature of our hearts (more precisely our souls) and must exercise vigilance against them. The Prophet (peace and blessings be upon him) taught us that “Hearts are ever changing.” The word “qalb” in Arabic means twisting and turning. So we must take care to dismiss and reject bad thoughts that assail our hearts and minds by ever turning to Allah for refuge and protection. We must also recognize that ultimately we have no power to master these evil inclinations except through the help of Allah. The remedy and cure therefore lies in consistency in dhikr (remembrance of Allah) and istighfar (seeking Allah’s forgiveness).

The Prophet (peace and blessings be upon him) taught us the following du`a’s (supplications) to master the evil inclinations of our souls, and, accordingly, these must form part of the daily spiritual regimen of every believer:

Allahumma ya muqalliba al-qulub thabbit qalbi `ala dinika. (O Allah, O You Who are the twister of hearts, so make my heart firm and steadfast on Your religion) (Al-Mustadrak)

Rabbi a`udhu bika min hamazati ash-shayatin 
(My Lord, I seek Your refuge and protection against the whisperings of devils). (Sunan Abi Dawud)

Finally, read the Sayyid al-Istighfar (master supplication for forgiveness) in the morning as well as before retiring to bed: Allahumma, anta rabbi, la ilaha illa anta; khalaqtani wa ana `abduka, wa ana `ala `ahdika wa wa`dika ma istata`tu; a`udhu bika min sharri ma sana`tu; abu’u laka bi ni`matika `alayya wa abu’u bi dhanbi; fa ighfir li, fa innahu la yaghfiru al-dhunuba illa anta. (O Allah, You are my Lord, there is no god but You; You have created me, and I am Your servant, and I stand firm on my covenant with You according to the best of my ability; I seek refuge in You from the evils of my own actions; I acknowledge Your favors upon me and I confess my sins to You; so forgive me, for no one has power to forgive sins except You.) (Sahih al-Bukhari)

Finally, examine yourself, and see if you have been guilty of any major sins; if you have been, make sincere repentance to Allah from all of them without delay.