Try resetting PRAM
http://support.apple.com/kb/HT1379
followed by resetting SMC (power management)
http://support.apple.com/kb/HT1543
which is basically disconnecting ALL cables and power, and then reconnecting following a precise procedure. Since this is power-related, consider how the iMac is connected to power. If it is plugged into an over-crowded or old power strip (surge suppressor), you may want to try connecting it directly to the wall outlet, at least initially (or get a new power strip and only connect the iMac to it). Initially power it up with nothing connected except the power cord, and then add just your keyboard/mouse. Use it that way for a while to see if this problem recurs. Then, if the problem has not recurred, add peripheral devices (USB/FireWire)... do it slowly if possible; peripherals can sometimes interfere with sleep, wake, shutdowns, and startups, so note if the problem recurs after adding any particular peripheral device.