I am having the same issue with my iPhone 3GS as well. The phone will do fine, but then out of the blue, it will turn itself off, reboot, and then a few minutes later, will shut itself off and reboot again. The only way I have been able to stop this from happening is to restore to factory settings. Granted it will sttay fixed for a few days, but then it starts back up again. This is really annoying. Any help would be mu appreciated. Thanks.
The only solution I could find was to change the battery. It seems that iOS5 will make your battery issues known if your battery is near the end of it's life. My phone would shut off several times a day.
If you're bold enough and don't mind ripping open your phone, I ordered one off of Ebay for $5 and it had every tool I needed in it. Also the detailed instructions can be found on ifixit.com. Works like a charm now, I haven't had any issues at all.
Feel free to contact me for any questions if you plan on changing your battery.