Praying While Resisting The Urge To Defecate
Question

Sometimes I need to defecate before I pray, but I pray then I do not feel that urge whilst I am praying. Is my prayer acceptable? And sometimes the opposite happens – is my prayer acceptable in that case?
Praise be to Allah.It is not permissible for a person to start praying when he is resisting the urge to urinate or defecate because the Prophet (peace and blessings of Allah be upon him) said: “You should not pray when food is ready or when resisting the urge to urinate or defecate.” (Narrated by Muslim) 
The reason for that – and Allah knows best – is that this prevents a person from having the necessary presence of mind and humility in the prayer. 
However, if a person prays in this state, his prayer is valid although it is somewhat lacking and is imperfect because of the Hadith quoted; he does not have to repeat it.  But if one starts to pray when he is not resisting the urge to urinate or defecate , then he starts to feel that need during the prayer, then the prayer is valid and there is nothing disliked in it, so long as that need does not prevent one from completing the prayer.
For more details, please refer to the following answers: 11591 , 174361 , 2137 , and 26280 . 
And Allah is the source of strength. May Allah bless our Prophet Muhammad and his family and companions, and grant them peace.