I left my computer sleeping but turned off the power by accident. Hours after the power was turned back on the computer booted itself up. It is plugged into a UPS. I don't understand how this could happen.
On the iMac, there's a setting under System Preferences > Energy Saver called Start up automatically after a power failure, which I'm guessing will be selected on your system, and hence would explain your computer's spontaneous rebooting.
If you don't wish this to happen in the event of a power failure, or the next time you turn off the power by accident, then uncheck that particular option.
As Tetsujin very helpfully points out in the comments below, there is also a button in the bottom right of the Energy Saver preferences pane labelled "Schedule...", which allows the user to regulate when the computer starts up, wakes up, or goes to sleep. It would be wise to double check your settings there too.