I'tikaf is a spiritual retreat to a masjid (mosque), usually during the last 10 days of Ramadhan. A person going to do i'tikaf will spend the evening and night in a mosque, devoting their time for worship like reading the Quran, supplications, Dhikr etc.
I’tikaaf is prescribed according to the Qur’aan and Sunnah and scholarly consensus. It’s a kind of worship to Allah alone. ’tikaaf in the mosque is an act of worship, and (observing it) in Ramadaan is better than at other times. It is prescribed in Ramadaan and at other times.
“…And do not have relations with them as long as you are staying for worship in the masjid. These are the limits [set by] Allah سبحانه و تعالى, so do not approach them. Thus does Allah سبحانه و تعالى, make clear His ordinances to the people that they may become righteous.”Qur’an.SurahBaqarah 2:187
Allaah says: “and We commanded Ibraaheem (Abraham) and Ismaa’eel (Ishmael) that they should purify My House (the Ka‘bah at Makkah) for those who are circumambulating it, or staying (I’tikaaf), or bowing or prostrating themselves (there, in prayer)”Surah al-Baqarah 2:125
Narrated by Abu Salama رضي الله عنه:I asked Abu Sa'id رضي الله عنه, and he was a friend of mine, (about Laylat al-Qadr) and he said, "We practiced Itikaf (seclusion in the mosque) in the middle third of the month of Ramadan with the Prophet ﷺ. In the morning of the 20th of Ramadan, the Prophet ﷺcame and addressed us and said, ‘I was informed of (the date of the Night of Qadr) but I was caused to forget it; so search for it in the odd nights of the last 10 nights of the month of Ramadan. (In the dream) I saw myself prostrating in mud and water (as a sign). So, whoever was in ltikaf with me should return to it with me (for another 10-day period),’ and we returned. At that time there was no sign of clouds in the sky but suddenly a cloud came and it rained till rainwater started leaking through the roof of the mosque, which was made of date-palm leaf stalks. Then the prayer was established and I saw Allah's Apostle ﷺprostrating in mud and water and I saw the traces of mud on his forehead." Sahih Al Bukhari Vol; 3:233
The basic principle is that i’tikaaf is Sunnah, not obligatory, unless one made a vow to do it, in which case it becomes obligatory, because the Prophet (peace and blessings of Allaah be upon him) said: “Whoever vows to obey Allaah, let him obey Him, and whoever vows to disobey Him, let him not disobey Him.” Sahih al-Bukhari, 6696
And ‘Umar (may Allaah be pleased with him) said: “O Messenger of Allaah, during the Jaahiliyyah I vowed to observe i’tikaaf for one night in al-Masjid al-Haraam.” He said: “Fulfil your vow.” Sahih Bukhari 6697
Ibn al-Qayyim (d. 751H) رحمت اللهsaid
"Allah سبحانه و تعالى,also prescribed i'tikaffor them, the objective being that the heart becomes fully preoccupied with Allah سبحانه و تعالى, concentrated upon Him alone, and cut-off from being preoccupied with the creation. Rather, the heart is only engrossed with Allah سبحانه و تعالى,such that loving Him, remembering Him, and turning to Him takes the place of all the heart's anxieties and worries, so that he is able to overcome them. Thus all his concerns are for Allah سبحانه و تعالى,, and his thoughts are all directed towards remembering Him and thinking of how to attain His Pleasure and what will cause nearness to Him. This leads him to feel contented with Allah سبحانه و تعالى,, instead of people. This in turn prepares him for being at peace with Allah سبحانه و تعالى,alone, on the day of loneliness in the grave, when there will be no one else to give comfort, nor anyone to grant solace, except Him. So this is the greater goal of i'tikaf."