In The Name of Allaah, The Most Merciful, The Bestower of Mercy.
Allaah [The Exalted] said:
أَمۡ حَسِبۡتَ أَنَّ أَصۡحَـٰبَ ٱلۡكَهۡفِ وَٱلرَّقِيمِ كَانُواْ مِنۡ ءَايَـٰتِنَا عَجَبًا
إِذۡ أَوَى ٱلۡفِتۡيَةُ إِلَى ٱلۡكَهۡفِ فَقَالُواْ رَبَّنَآ ءَاتِنَا مِن لَّدُنكَ رَحۡمَةً۬ وَهَيِّئۡ لَنَا مِنۡ أَمۡرِنَا رَشَدً۬ا
فَضَرَبۡنَا عَلَىٰٓ ءَاذَانِهِمۡ فِى ٱلۡكَهۡفِ سِنِينَ عَدَدً۬ا
ثُمَّ بَعَثۡنَـٰهُمۡ لِنَعۡلَمَ أَىُّ ٱلۡحِزۡبَيۡنِ أَحۡصَىٰ لِمَا لَبِثُوٓاْ أَمَدً۬ا
Do you think that the people of the Cave and the Inscription (the news or the names of the people of the Cave) were a wonder among Our Signs? (Remember) when the young men fled for refuge (from their disbelieving folk) to the Cave, they said: “Our Lord! Bestow on us mercy from Yourself, and facilitate for us our affair in the right way!” Therefore, We covered up their (sense of) hearing (causing them, to go in deep sleep) in the Cave for a number of years.Then We raised them up (from their sleep), that We might test which of the two parties was best at calculating the time period that they had tarried. [Ayaat 9-12]
[أَمۡ حَسِبۡتَ أَنَّ أَصۡحَـٰبَ ٱلۡكَهۡفِ وَٱلرَّقِيمِ كَانُواْ مِنۡ ءَايَـٰتِنَا عَجَبًا -Do you think that the people of the Cave and the Inscription (the news or the names of the people of the Cave) were a wonder among Our Signs?]- Meaning: Their affair was not something amazing in relation to Allaah’s All-Encompassing Power and Authority Over all affairs, for indeed the creation of the heavens and earth, the alternation of night and day, the subjugation of the sun, moon, the stars and the other mighty signs demonstrates Allaah’s All-Encompassing Power. Allaah is able to do whatever He wills and nothing [in the heavens or in the earth] escapes Him. Ibn Jurayj [rahimahullaah] reported from Mujaahid [rahimahullaah], who stated about the meaning of the above Aayah: You are amazed by the story of the people of the cave! Indeed, there are among the signs of Allaah that which is more amazing than that. (1) Allaah has not ceased showing His slaves signs in the universe and in their ownselves, by way of which truth is distinguished from falsehood, and guidance from misguidance. (2)
[إِذۡ أَوَى ٱلۡفِتۡيَةُ إِلَى ٱلۡكَهۡفِ- (Remember) when the young fled to the Cave]- Meaning: Intending to fortify and shield themselves from the Fitna of their people. [فَقَالُواْ رَبَّنَآ ءَاتِنَا مِن لَّدُنكَ رَحۡمَةً۬ – they said: Our Lord! Bestow on us mercy from Yourself]-Meaning: Make us steadfast by way of it, protect us from evil and bestow good upon us.
[وَهَيِّئۡ لَنَا مِنۡ أَمۡرِنَا رَشَدً۬ا – And facilitate for us our affair in the right way!]- Meaning: Facilitate for us every means that will lead us to the right way and make our religious and worldly affairs upright.
So, they hurried away and fled from Fitna [and went] to a place where they can hide. They supplicated to Allaah with humility to make their affair easy and did not merely rely upon themselves and others; and due to this, Allaah answered their supplication and brought about for them that which they could not expect. Allaah said:
[فَضَرَبۡنَا عَلَىٰٓ ءَاذَانِهِمۡ فِى ٱلۡكَهۡفِ سِنِينَ عَدَدً۬ا -Therefore We covered up their (sense of) hearing in the Cave for a number of years]- Meaning: Allaah made them sleep for three hundred and nine years. And by way of this sleep, Allaah protected their hearts from confusion [or corruption] and fear, and from [the fitna] of their people; so that they might be a manifest sign [to the people].
[ثُمَّ بَعَثۡنَـٰهُمۡ لِنَعۡلَمَ أَىُّ ٱلۡحِزۡبَيۡنِ أَحۡصَىٰ لِمَا لَبِثُوٓاْ أَمَدً۬ا – Then We raised them up (from their sleep), that We might test which of the two parties was best at calculating the time period that they had tarried]- Meaning: Which of them could calculate the time period they stayed in the cave, just as Allaah said: [وَڪَذَٲلِكَ بَعَثۡنَـٰهُمۡ لِيَتَسَآءَلُواْ بَيۡنَہُمۡۚ – Likewise, We awakened them (from their long deep sleep) that they might question one another].
The knowledge regarding how long they slept in the cave demonstrates precision in calculation, knowledge of Allaah’s All-Encompassing Power, Wisdom and Mercy. And had they continued to sleep, nothing would have been known regarding their story. (3)
[Ref 1: An Excerpt from Al- Misbaahul Muneer Fee Tahdbeeb Tafseer Ibn Katheer]
[Refs 2 & 3: An Excerpt from Tayseer Al-Kareem Ar-Rahmaan Fee Tafseer Kalaam al-Mannaan. slightly paraphrased]