Most tech that does this has a clock that keeps running when the device is off. On laptops and computers it’s typically powered by a small battery on the motherboard, or sometimes a supercapacitor. Phones will have this circuit, but I think they just run off the battery, they might use a supercap, but I don’t know of any with an extra battery for this.
Not everything actually has that though, a raspberry pi for example doesn’t have one, so when they boot they have no idea what the time is, and typically are configured to just get the time when it gets internet access (when they can find another computer on the internet and ask it for the time). For a lot of internet things, this is how they work (routers, google home, alexa, etc).