iPhone 3g (iOS 3.1) screen rotation problem
The process in charge of reading the accelerometer may be having troubles. I suggest you hold on your power/suspend button for 5 secs and then completely turn off your iPhone. After the phone has been turned off, press the power button again to start it (this will perform a "cold boot" of the phone). That's the equivalent of completely powering off a computer. 

If the problem persists after the reboot, then you might have to restore your phone from a backup using iTunes. 

Please provide more information or use the search tool in the upper right corner of the website to find similar answers on how to restore an iPhone from a Backup.