I had something similar when a friend spilled a sweet drink on it. We were out in a club, and I placed my phone on a table --> drunkard spilled a cocktail on it.
In my case, cleaning it as good as possible (and having the phone in a rice bath didn’t solve it.
But plugging in and out and in and out … several times … the headphone check solved it.
Those headphone jacks break hardly, these are made to last. Maybe you have something stuck in there that tricks your phone into thinking a headphone is connected.