How come Mohammad (pbuh) was married to 9 women while Islam allows men to have up to four wives? Is there a reason behind that?
We don't know the exact number of Muhammad (pbuh) wives but we know that they were more than four. It was allowed by Allah and this was exclusive for him as clearly stated in Quran (verse 33:50):
يَا أَيُّهَا النَّبِيُّ إِنَّا أَحْلَلْنَا لَكَ أَزْوَاجَكَ اللَّاتِي آتَيْتَ أُجُورَهُنَّ وَمَا مَلَكَتْ يَمِينُكَ مِمَّا أَفَاءَ اللَّهُ عَلَيْكَ وَبَنَاتِ عَمِّكَ وَبَنَاتِ عَمَّاتِكَ وَبَنَاتِ خَالِكَ وَبَنَاتِ خَالَاتِكَ اللَّاتِي هَاجَرْنَ مَعَكَ وَامْرَأَةً مُّؤْمِنَةً إِن وَهَبَتْ نَفْسَهَا لِلنَّبِيِّ إِنْ أَرَادَ النَّبِيُّ أَن يَسْتَنكِحَهَا خَالِصَةً لَّكَ مِن دُونِ الْمُؤْمِنِينَ قَدْ عَلِمْنَا مَا فَرَضْنَا عَلَيْهِمْ فِي أَزْوَاجِهِمْ وَمَا مَلَكَتْ أَيْمَانُهُمْ لِكَيْلَا يَكُونَ عَلَيْكَ حَرَجٌ وَكَانَ اللَّهُ غَفُورًا رَّحِيمًا
O Prophet, We have made lawful for thee thy wives whom thou hast given their wages and what thy right hand owns, spoils of war that God has given thee, and the daughters of thy uncles paternal and aunts paternal, thy uncles maternal and aunts maternal, who have emigrated with thee, and any woman believer, if she give herself to the Prophet and if the Prophet desire to take her in marriage, for thee exclusively, apart from the believers -- We know what We have imposed upon them touching their wives and what their right hands own -- that there may be no fault in thee; God is All-forgiving, All-compassionate.
Scientists believe that it surely wasn't driven by lust, because he could have done it when he was younger. But it was done after his Prophethood to advertise the religion. He married a forty year old woman -Khadijeh- at 25 and lived with her for twenty four/five years until she died. So it was all done for the benefit of Islam and due to his long term goals.
You're right that Muslims aren't allowed to have more than 4 wives. In fact the Prophet's followers did question his fifth marriage on those grounds and also because his fifth marriage was to a woman who was married to prophet's adopted son as per Qur'an 33:37. And then Muhammad(pbuh) had a revelation that he can marry more than 4 times as mentioned in sura 33:50.
O Prophet, indeed We have made lawful to you your wives to whom you have given their due compensation and those your right hand possesses from what Allah has returned to you [of captives] and the daughters of your paternal uncles and the daughters of your paternal aunts and the daughters of your maternal uncles and the daughters of your maternal aunts who emigrated with you and a believing woman if she gives herself to the Prophet [and] if the Prophet wishes to marry her, [this is] only for you, excluding the [other] believers. We certainly know what We have made obligatory upon them concerning their wives and those their right hands possess, [but this is for you] in order that there will be upon you no discomfort. And ever is Allah Forgiving and Merciful. (Qur'an 33:50)
The Qur'an restricts all Muslims from having more than 4 wives but the prophet is exempt from this restriction. This rule hasn't been adhered to by his successors like Ali. Most of the prophet's later marriages were an attempt of attaining truce between him and his enemies. And marriage did bring peace with his enemies. However, the fact that he had the option of adopting these women as his daughters instead of getting married to them has been questioned by his critics. Aisha is considered Muhammad's favorite and youngest wife is an exception though, as her marriage with the prophet isn't considered to have political motives.
Nabi Muhammed (pbuh) married at age 25 with Khadija. Khadija was 40 years old at that time. Khadija herself offered the marriage. Nabi Muhammed lived 25 years with Khadija, Khadija died at the end of these 25 years. Then he married his second wife Sawda. Then the multiple marriages started.
If Muhammad were to marry for lust, why would he stay with one wife until his old age, and married with more more women at old age?
We mustn't think Muhammad as ourselves. He was a prophet. He was center of a new information flow that will someday spread to all over the world. He had a lot of social duties both among men and women. His wives became his students, they became teachers of Islam among other women. Muhammad's marriages were not because of lust, wealth, or other kind of worldly desires. They had some other good purposes.
The Prophet (SAW) only had four wives. He did not marry again while his four wives were with him but only to complete four again when one passed away. He did this to help the woman and to support her.
In Islam a man is allowed up to four wives, however a woman is only allowed one husband. If the woman becomes a widow she is allowed to re-marry just as men are allowed to do so too.
So the answer to your question is yes you are allowed to marry more than four time but you cannot have more than four wives (or more than one husband if you are a woman).
Please also note that there are conditions you have to meet if you want another wife e.g. your first wife had to agree to your decision.