The is an easy test and solution.
Remove three screws on battery connector. Unplug the charging port connector (not the battery). After that leave it for a while to see if its doing the same thing.
If it stops doing that just change the whole charging port assembly. Costs $7 i guess.
Charging port assembly has microphone, speaker, antenna, headphne jack and home button connectors in it.
If its doing the same thing after disconnecting the charging port connector it may have mainboard problem (water damage).
Good luck ...