The infallibility of the Messengers [of Allāh] [may the peace and blessings of Allāh be upon all of them] is a subject that contains that which is wholly agreed upon [by the scholars], both at the inception of the discussion and at its culmination. This issue also pertains to that which there is difference of opinion [among the scholars], although this [difference] exists only at the beginning of the discussion, rather than at its end. This is made clear [in two points]:
The consensus of the scholars is that the Messengers [of Allāh] have attained perfection with regards to what they have related to us from Allāh, and in their complete delivery of their respective messages. This is because it is only by this perfection that the mission of the message and prophethood can be fulfilled.
There is a difference of opinion [among the scholars] concerning the occurrence of disobedient acts among the Messengers. Some [scholars] adopt the opinion that they are completely infallible from both major and minor sin. This is justified by the fact that the station of prophethood is exalted above such errors and intentional disobedience to Allāh. Also, we have been ordered to emulate them, which would be impermissible whilst they engage in acts of disobedience. Rather, the command of emulation stipulates that they only engage in acts of obedience. [Those who take this opinion] funnel the interpretation of all the aḥādīth and āyāt that confirm error with consideration of these justifications. However, the consensus of the scholars is that minor sin can occur from them. Their evidence is from the Qurʾān and the stories of the Messengers as [related in the Sunnah]. This view has the caveat that no Messenger intentionally persists in error. Rather, they repent to Allāh from it and never return to it.
Thus, their infallibility applies only to them not persisting in error, and our emulation of them delineates repentance [when we commit sin].