Panic can also be caused by CO2.
CO2 retention/build up can be caused by a lot of the things y'all talk about. Simple as swimming too hard in a current and over breathing your regulator to holding your breath whilst trying to get 'just the right shot' or a change in demand, such as happily skip breathing along (consciously or unconsciously) and then whatever happens and you start expending more energy (say swimming around looking for a lost buddy) and added to the already mild CO2 retention from the previous skip breathing the increased motion and not always increased breathing rate (I tend to hold my breath for a few seconds to see if i can hear my buddies bubbles) well... you get the picture.
and yes, this is me beating an incredibly dead horse.
BUT, the story i'll tell again and again is the one about scrubbing for awake carotid surgery in the operating room. Patient is fine fine fine (a nice sedated fine I'll add) and then when the surgeon puts a vascular clamp on the carotid near the bifurcation, on occasion he would put it right across the carotid bodies, (little chemo receptors that monitor O2 and CO2) http://en.wikipedia.org/wiki/Carotid_body
At that exact moment, with no other changes, we could watch the patient have a massive panic attack. We'd release the clamp and voila... back to normal. The surgeon would then move the clamp around to do his best to avoid them and proceed with surgery. As it was explained to me, when you put a clamp on the chemoreceptors, you can stimulate them and they send the same messages to the brain as if they were experiencing a massive CO2 event.
Anyhow, how to combat it... A lot of what everyone says holds true, but another very important method is to get fit for diving. And simply be aware, I always think that if you practice mindful, self aware diving you'll suddenly start to notice more both in yourself and in your surroundings. You'll see more, hear more, experience more. One benefit of that is you can feel the niggle coming on before it turns into something you actually have to 'manage'.
Prevention is the best medicine.
I'm not always sure that the advice to get breathing 'under control' with slow deep breathing is the best, but more importantly, think about good gas exchange utilizing your whole lungs. If you are holding your breath to take that picture or hover on the wall or to try to maintain perfect trim for the photo, knock it off. If you are hyperventilating and unable to keep up with demand cause you are swimming around madly in nervous circles looking for you buddy, or desperately trying to keep up knock it off (signal them to slow down!!! use that nice big light you spent cha-ching on). If you are caught in a bit of current and unable to maintain a respiratory rate for adequate gas exchange then dump some gas, find a rock to hold on to and chill for a min while your body can catch up.
and yes, all the other stuff people talked about is 100% valid, I'm not discounting any of it at all or saying CO2 is the only thing that causes panic, I just think that it has a bigger role in this party than we give it credit for
Oh, one other thing... the brain remembers that feeling... the feeling of panic... with remarkable clarity. If you have experienced this, you know. Tincture of time and easing back into things. It will still be there, it will just get less and less until one day its gone.