My 5yo son does the same thing! It's as if he's caught in the middle of being almost grandiose about himself at times (talking back," Im faster, better,stronger, than anyone") then being painfully shy and not confident at all. Same thing at parties and events. Wants to go until we get there, then either wants to leave or have my husband and me play with him at the party instead of other kids, even hiding behind my legs. Once he covered his ears the whole time Happy Birthday was sung. I've found that getting to whatever event a little early helps-that way he's warming up to people as they arrive and is a part of the party before it's in full swing. Then it's great to see him be being part of the fun. Sometimes it takes awhile, even half an hour or more, but he always ends up having fun. Let him know you are there (if parents are to stay), look around and speak to other people with him, then as he gets more comfortable encourage him to play with others. I have found myself "hovering" too much sometimes, so am trying not to do that anymore. Good luck!
Perhaps you are trying too hard Back off and let him make his own choices,a lot of kids say they want to do stuff,then dont want to.Go easy on the pressure for him to do anything, at partys and socila gatherings how about you let him stay alone without you guys there .it could be he wants to please you and is aware of too much interest.
I would talk to him maybe at home or somewhere private he is comfortable one on one, and ask what it is that bothers him about those situations. It sounds he might be fearful of crowds, which can be very normal. At his age he is still building his self esteem, if you are very shy, he could be seeing you withdraw from that somewhat. Ask him what would help him be more comfortable, build up the things he can do if he trys and assure him you will be close by cheering him on. Children want their parents to be proud of them, maybe even start a small reward for his efforts each time.