The short answer is that this is unknown. However, there are a few potential leads.
One hint is that people are able to estimate the passage of time fairly reliably, and this ability seems to persist during sleep (Ukraintseva, Liaukovich, & Shilov, 2021; Aritake-Okada et al, 2010):
The notion that [time estimation ability] operates in the brain even
during sleep is supported by, for example, “self-awakening” ... and
“anticipated sleep termination” ... This research found that ...
subjective elapsed time during sleep was very close to actual sleep
time ...
Furthermore, the ability to estimate time during sleep can vary substantially. Moorcroftl & Breitenstein (2000) review current speculation:
Source of self-awakening ability
In addition to imputed read-out of an internal biological clock,
speculation about the source of this awareness of time during sleep
has also included light sleep with frequent awakenings and periodic
REM sleep. ... Bell has proposed that the ability to self-awaken
consists of two components: the ability to induce more frequent
awakenings coupled with the ability to use these awakenings to mark
the passage of time.
Accordingly, successful self-awakening is associated with factors that increase "arousal" (alertness) during sleep such as lighter sleep, shallower sleep phases (ie, REM), lower waking threshold, increased brain activity, reduced sleep inertia (grogginess), and higher sleep quality (Ikeda & Hayashi, 2012; Matsuura & Hayashi, 2009; Malloggi et al, 2022). This presumably increases both time estimation accuracy and ease of waking. Hayashi, Matsuura, & Ikeda (2010) summarize current findings with this schematic:
Additionally, one study suggests that successful self-awakening is associated with increased brain activity particularly in the right prefrontal cortex, which incidentally, is involved in time estimation (Aritake et al, 2012).