I'm surprised with all these answers that no one has mentioned this. I know several people with similar problems, but they are all caused by different things and they all deal with it differently. So let's look at it from a few angles.
It sounds like you don't want to discuss the specifics here, but you do mention it's related to mental health. I'll give a couple ideas from that perspective, but I'll also include a couple others for the sake of future readers (since, as mentioned, this has many causes).
The key to handling all of these is empathy. We need to make whoever is questioning your behavior empathize with your situation, otherwise they likely won't be satisfied with anything you say. But in order to evoke empathy, you have to give up at least a tiny bit of personal information. Have no fear, we can do this without revealing anything terribly embarrassing.
1) Phobias
A number of phobias can cause someone to dislike large social gatherings. Claustrophobia, agoraphobia, etc. All can lead to anxiety, resulting in tension, headaches, nausea, feeling unable to breathe, and so on. Even mysophobia (more commonly known as "germophobia") can come into play. I know someone who usually can't get close to people because they can "see" the germs expelled when other people breathe.
The problem with phobias is they usually aren't taken very seriously by society. They're an "informal" diagnosis, that some less-sensitive people think you can just "get over". In this case it's better to focus on the anxiety aspect, which most people can relate to on some level. You can usually just leave it at that without further prodding, and it's not a very embarrassing thing to admit in this day and age where mental health is becoming a bigger concern. For example:
"What's wrong?"
[smile] "Sorry, crowds just make me anxious. I'll be okay, don't worry."
I bet you'd be hard-pressed to find someone who won't be sympathetic to that, and very few people would take it personally. If pressed on why you attended you can just follow up with:
"I'm here to see [you/them/whoever], it's worth it to me."
2) Anxiety
This really has the same solution mentioned in #1, but it's worth spending a minute discussing anxiety and why you shouldn't be afraid of admitting to it. Anxiety, in various forms, is a very common and accepted medical diagnosis. Many people struggle with it daily, even if there's no specific trigger ("generalized anxiety"). Some can get by without needing help, but if it impacts their life significantly, there is a huge variety of treatment options. One possible solution if you want to kill it and avoid the headache could be getting a prescription for an anxiolytic like lorazepam, which is a very common treatment for acute anxiety. Take one before going to the party and you'll likely feel much better and maybe even enjoy yourself.
Of course all that only applies if anxiety is your problem (which it is in these cases 95% of the time, regardless of what causes it).
3) Hyperacusis
You mention headaches, so I wanted to touch on this briefly in case it's the cause. This is one that's not well-known to most people, but it is surprisingly prevalent. In a nutshell, hyperacusis is a sensitivity to noise or certain types of noise. In severe cases it can be debilitating, but often it simply causes headaches, irritation, and is generally very uncomfortable. If this is your problem, congratulations, you now know the name for it! It's really not an embarrassing one to talk about, simply giving a quick explanation should be enough to put most people at ease:
"What's wrong?"
"I've got this thing called hyperacusis, basically loud noises give me a bad headache. I'll be okay, I just need some air once in a while."
4) Complex mental disorders
I saved this for last because it's a trickier one to deal with, and I can imagine why you would be uncomfortable disclosing anything about it. There are several disorders that fall into this category, but you can treat them all the same in this case. We're talking about things like schizophrenia, schizo-affective disorder, bipolar disorder, borderline personality disorder, various flavors of dysmorphic disorder (like the kind that make other people/objects mutate into terrible things), the list goes on and on.
Long story short, any of these can cause major discomfort in social situations that are out of your control, and can make it easy for you to be overwhelmed by the sensory stimuli around you. It's perfectly normal to want to avoid social gatherings in this case, so don't feel bad about that. If this is the problem, you can still handle a concerned host/friend without giving too much away. This is one of the easiest approaches:
"What's wrong?"
"I'm okay, big crowds just make me uncomfortable. Too much noise and commotion, you know."
The first part of that response is a little cryptic and might spawn further questions, so adding a pinch of detail (substitute whatever symptom you think applies to you best) should be enough to evoke that empathy from them. Notice how it's not terribly different from the previous suggestions, they're all intentionally vague but reveal just enough personal detail to get the questioner to understand.
The exact detail you reveal depends on what kind of problem you actually have, but it keeps you from having to lie or mislead anyone. More than likely, whoever you tell it to will remember it in the future and not interrogate you further, beyond just checking to make sure you're okay.
tl;dr
If these people actually care about you at all, a tiny dose of honesty will be enough to gain their empathy and prevent further questioning. No need to lie or be awkward about it, or worry about revealing too much.