I was having this exact issue with my iPhone 4. I couldn't find a cure for it, and mine was really random, i couldn't recreate the issue at Apple store too, so they wouldn't help me to fix the phone. What i was doing to cure it for a period of time was to hard reset the phone. But once i update the iOS to 4.3.4 back at those time, the issue was gone for ever.
I'm not sure if it's the iOS which is corrupted, where a factory restore could be able to help it, or if it's typically related to the hardware.
Now you can hard reset the iPhone, try to blow wind in the earphone outlet.