Praise be to Allah.
I’tikaaf means staying in the mosque.
I’tikaaf is Sunnah and mustahabb, especially in the last ten days of Ramadaan, but it is not obligatory unless a Muslim has made it obligatory for himself by means of a vow. But if there is no vow, it is not obligatory.
See the answer to question no. 48999.
The basic principle is that the person who is observing i’tikaaf should not exit the mosque except in the case of a need which cannot be met in the mosque, such as wudoo’, ghusl and relieving himself, because of the hadeeth of ‘Aa’ishah which says that when the Prophet (peace and blessings of Allaah be upon him) observed i’tikaaf, he did not enter the house except to relieve himself. Narrated by Muslim (297).
If you need to go to the doctor and this appointment cannot be delayed until after Ramadaan, then it seems that there is nothing wrong with your exiting the mosque to go to the doctor, then coming back to the mosque. Al-Nawawi (may Allaah have mercy on him) stated in al-Majmoo’ (6/545) that for a person in i’tikaaf who is so sick that it is too hard for him to stay in the mosque because he needs to be in his bed, and he needs to be helped and to be visited by the doctor etc, it is permissible for him to leave the mosque. End quote.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) said in Jalasaat Ramadaaniyyah (1411 AH/ al-Majlis al-Saabi’, 144):
Whoever needs to see a doctor may leave, otherwise he should stay in the mosque. End quote.
And Allaah knows best.