Ibn al-Mundhir said: The people of knowledge have agreed that the person in i'tikaaf has the right to leave the place where he is observing i'tikaaf for the purpose of urinating or defecating.
This is because one cannot escape this. It is not possible to do this in the Masjid. Had the i'tikaaf become baatil because of this, then no ones i'tikaaf would be valid. And because the Prophet (sallallaahu alayhi wa sallam) used to observe i'tikaaf and we know that he would exit for the purpose of fulfilling his needs.
And that which is meant by fulfilling the needs of man is urinating and defecating. It is used metaphorically to mean both of them. Because every person has a need for [freeing himself] from them.
And included in the meaning is eating and drinking. When he has no one who can bring him his food and drink then he has the right to leave for them when he needs to. And if he needs to vomit, then he can leave to vomit outside the masjid and everything which one cannot escape [he can leave the masjid to do so].
His i'tikaaf does not become invalid because of leaving for it as long as he does not leave for a very long time. And likewise he can leave for that which Allaah has made obligatory for him like the person who performs i'tikaaf in a masjid where Friday prayer is not held and he needs to leave the Masjid to pray the Friday prayer [then he can do so]. His i'tikaaf does not become invalid. And this is also the opinion of Abu Hanifah.
al-Mughni by Ibn Qudaamah Volume 3 Page 192