Islam teaches that the Prophets of God (peace be upon them all), are people selected by God Himself to deliver His message to mankind.
The Muslim belief that Prophets are sinless primarily relates to the fact that the Prophets of God delivered His message in its purest form. They did not add anything to it, on their own, and neither did they hold anything back from it.
The Qur’an says that Muhammad (pbuh) is a true Prophet of God and if he had falsely ascribed something to God, he would have been punished like no other individual.
Had he invented lies concerning Us, We would have seized him by the right hand and severed his heart’s vein; and no one from amongst you could have protected him (from Us). … (Al-Haaqqah 69: 44 - 47)
The Qur’an also tells us that to supervise the unadulterated deliverance of the message, the prophets are closely observed by God.
As for the messengers whom He has chosen, He guards them from the front and from behind to make sure that they have delivered the messages of their Lord. … (Al-Jinn 72: 27 - 28)
Besides this, Muslims also believe that because God Himself, on the basis of His all encompassing knowledge, selected these prophets for delivering His message and to become a model for their followers, therefore these prophets must have been the best of mankind living in their particular times and societies.
However, this does not mean that the prophets do not commit any mistakes because as we know, mistakes are not synonymous with sin.
These prophets may commit mistakes, which are corrected by God, but these mistakes are not sins. Even these mistakes are in the way of righteousness.