How does an iOS device keep time when it is out of power?
Solution 1:
The iPhone is not completely depleted when the battery "dies." If you'll notice, when you try to turn it on when the battery is dead, the screen will come on briefly to tell you the battery is dead -- i.e., there is still some energy left in the battery. The clock continues to run off of the battery, and takes such a small amount of energy that Apple decided to keep the clock going.
Eventually, the battery will completely die (probably after many days or weeks), and the clock will eventually stop keeping time. As Ian C. mentioned, the clock will synchronize when the phone does get plugged in and turned on again, if a network is available.
Solution 2:
The phone synchronizes its clock with your wireless provider's embedded clock signal when it starts up. All cellular protocols provide for a clock network feature that lets devices on the network synchronize their time and date with a master time and date from the provider.
So it isn't that the phone kept time while it was out of power, it was that it set the correct date and time when started up.