I suspect that your problem is that you have some sort of foreign material in the headphone jack. This is more common for guys, as we tend to put our phones in our pockets, and lint gets into the jack. I've also had it happen that I get my pants or shorts wet with salt-water when I'm walking along the beach where I live. If some moisture were to make its way into the headphone jack, it's going to leave a microscopic layer of salt behind. That salt, in turn, will attract moisture out of humid air, further challenging the headphone jack switching circuit and causing a false switch out of the speakers or microphone. I expect that the salts from perspiration can cause the same effect.
Regardless of the cause of foreign matter or contamination in the jack, I've had good results with the following procedure to clean out the jack. I've done this for USB and charging ports, too. This has worked for me on a few different models and other devices, too.
First, power off the phone. Hold the phone upside down with the jack point down. Then use a vacuum cleaner on its highest setting, with a hose and brush attachment, and cram that brush into the jack or port. Work it in and out and twist it around, back and forth, a few times. Then work a headphone plug into the port a few times, then back to the vacuum and brush.
Then spray some electronics cleaner into the jack. (You can usually buy it at local auto parts stores.) Work the headphone plug in and out again a few times while the cleaner is still wet in the jack. Then spray again and follow up with the headphone plug again. Finally, vacuum once more to help pull put any remaining cleaner from the jack.
Wait a few minutes for any residual cleaner to dry out before turning the phone on again. If the jack doesn't smell like cleaner any more, then I'm sure it's safe to power on, but ten minutes should be more than enough time if you're unsure.
Let us know if this helped. Good luck!