We had the same problem on one of our work computers. It was a newer Imac I5 Here is what I did.
1) Check your Console (located in Applications/Utilities) if you know roughly when it shut down, then odds are it's in the log, if not, it could indeed be a hardware problem, in our case it appeared to be software related since the Imac checked out ok at Apple Store.
2) Notice what apps are running at the time of the shutdown, and again check the Console
3) In this case, we had a program we had removed that was still in the LaunchDeamons, in fact I found many startup items that where either dead or outdated. If you know Macs, it is very difficult cleaning out residual files left by a deleted Application. So far under multiple tests, the computer is no longer turning off the screen by itself. Just do not delete what you are not sure of, it can mess up your computer worse. In our case it was simple since the file name corresponded to a App no longer on the machine which was obvious.
4) After I deleted all the old files, I ran Disk Utility Repair tool, and so far so good. (it would shut off intermittenly anywhere between a few minutes and an hour), after 6 hours it has stayed on just fine under multiple tests.
5) I also did some research on another rogue core function that sends the computer into a hibernate mode, the Imac was showing a previous shutdown code of 5 which seems to be the same as when you shut the lid on a macbook, that is the next thing I am checking on if the problem re-occurs again.