Imam Muhammad Al-Khidr Hussain (d. 1377), may Allah have mercy upon him, said the following:
The prophets, peace be upon them, are indeed infallible from all sins as they are prevented from committing such acts by way of proof and indication. The Exalted One said in the matter of the Prophet Yusuf, peace be upon him,
Had he not seen a sign from his Lord, he would have succumbed.
So they are rewarded for this striving as they prevent themselves from temptation and they abandon falling into all acts of disobedience.
(extracted from: Majallat ul-Islam, no.6, Year 8)
 Surah Yusuf (12), ayah 24