The prophets have to be ma'sum for the same reason for which they were sent: to guide and lead the people towards God. God, who decided to guide mankind, also intended to send immaculate and perfect human beings as models and examples.
If they were not ma'sum, then it would have been extremely difficult even to believe in the message let alone the example they present to us. There would be no trust or confidence in whatever they say: it could be true; it could be false.
Sending fallible prophets would have defeated the very purpose for which they were sent: for the prophets to lead and guide, for the people to follow and obey.
The Qur'an also supports this reasoning.
وَمَا أَرْسَلْنَا مِنْ رَسُولٍ إِلَّا لِيُطَاعَ بِإِذْنِ اللَّهِ
“We did not send any prophet, except so that he may be obeyed by the will of Allah.” (4:64)
The verse very clearly presents the divine social order: the sole purpose of the prophets on this earth is to be obeyed by their followers, not that the followers are to check every action and statement of their prophet and then decided whether they should obey or not.
Such absolute obedience cannot be possible unless the prophets were ma'sum, free from sins and mistakes.
يَا أَيُّهَا الَّذِينَ آمَنُوا أَطِيعُوا اللَّهَ وَأَطِيعُوا الرَّسُولَ
“O you who believe! Obey Allah and obey the Messenger…” (4:59)
In this verse, Allah is ordering us to obey Him and the Messenger. There are at least ten other verses where Allah has used imperative forms to order the believers to obey the prophets and messengers. Besides these verses, there are also many verses where Allah describes the virtue of obeying the prophets, and the dire consequences of disobeying them.1
Secondly, in most such verses, Allah has mentioned obedience to Himself alongside with the obedience to His messengers. Actually, in one verse, obedience to the messenger is made synonymous with the obedience to Allah:
مَنْ يُطِعِ الرَّسُولَ فَقَدْ أَطَاعَ اللَّهَ
“And whoever obeys the Messenger has actually obeyed Allah.” (4:80)
Such assertion on part of Allah would have been impossible if the prophets and the messengers were not ma'sum and infallible. Otherwise, we would have found ourselves in an impossible situation: a non-ma'sum prophet or messenger exhorts us to do something which is wrong—should we follow or not. In both cases, we would be doomed. If we obeyed the prophet and committed the sin, then we would be guilty of disobeying Allah who has told us not to commit sins. If we disobeyed the prophet and refused to commit the sin, then we would be guilty of disobeying Allah who has told us to obey the prophets and messengers in an absolute way!
To the above verses, add those verses in which Allah forbids you to obey certain types of people who commit sins:
فَلَا تُطِعِ الْمُكَذِّبِينَ وَدُّوا لَوْ تُدْهِنُ فَيُدْهِنُونَ وَلَا تُطِعْ كُلَّ حَلَّافٍ مَهِينٍ
“So do not obey those who accuse you of lying…& do not obey any mean swearer…forbidder of good, one who steps beyond the limits, a sinner.” (68:8-10)
وَلَا تُطِعْ مِنْهُمْ آثِمًا أَوْ كَفُورًا
“And do not obey among them a sinner or an ungrateful person.” (76:24)
وَلَا تُطِيعُوا أَمْرَ الْمُسْرِفِينَ
“And do not obey the command of the prodigals…” (26:151)
When you put these verses alongside the previous verses, you will get the complete picture:
a) The prophets are to be obeyed unconditionally.
b) The sinners and wrongdoers are not to be obeyed.
c) The only logical conclusion is that the prophets are not in the categories of the sinners or the wrongdoers.