if it is haraam to harm yourself then how did the prophet pray till his feet were swollen.
All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
Your perception that what happened to the Prophet was a forbidden harm is incorrect. This is not at all a harm to the self; rather, this is a minor tiredness in order to achieve a great benefit. A merchant works very hard and strives his utmost in order to achieve a worldly benefit, and he does not regard the exhaustion he experiences in this regard to be a harm; the same thing applies here.
Even if we presume that this is a harm – which is not the case as we have already mentioned – the harm which results from some acts of worship in order to achieve the great benefit is tolerable, and it does not prevent from performing such acts of worship.
In Jihaad there is loss of one’s own self (death) and money; yet it is legitimate because it is done in order to make the word of Allah superior and higher.
Similarly, some minor pain or harm may happen due to prayer or fasting, like that which used to happen to the Prophet when his feet got swollen due to standing long in prayer. This is bearable because of the great benefits that one gets, such as the great effect in reforming the heart, getting closer to Allah, and enjoying calling upon Him, Glorified and Exalted be He.
For more benefit, please refer to Fatwa 91003.
Allah knows best.
You can search for fatwa through many choices