The first step in panic is feeling a lack of confidence in one's abilities.
An ability to swim fairly well is the first step toward confidence in the water. The next step is learning skin diving skills. A combination of swimming ability and mastering skin diving skills eliminates a feeling of being overly dependent on SCUBA equipment.
Skills left out of most classes (because some agencies don't include them in their standards) such as doff & don (removing all equipment on the pool bottom, swimming to the surface then returned to put it all back on) and bail out (stepping into the water while holding one's gear and putting it on during descent) also build self confidence and help to eliminate that first step toward panic.
The second step in panic is when something goes wrong. We can train and practice and maintain our equipment, but we can never eliminate problems arising. When things go wrong, a diver will either have confidence in their ability to handle the problem or they won't. If they do have confidence, they'll solve the problem and move on.
If they lack confidence, they will become scared and as a result start to hypoventilate. Hypoventilation is rapid shallow breathing. It is often, incorrectly, called uncontrolled hyperventilation. These are the next two steps in panic. Hypoventilation causes a buildup of CO2 in the lungs. This is because shallow breathing does not purge the lungs of CO2 on exhalation. As CO2 builds, the urge to breathe becomes even stronger. Hypoventilation increases, making the feeling of not being able to breathe even worse. This is what makes some divers think they are over breathing their regulator. Once hypoventilation starts, full panic is often seconds away. Once the diver is panicked, he will often take inappropriate and usually dangerous actions such as bolting to the surface.
An understanding of this cycle can allow a diver to break it and prevent panic. Most agencies teach divers, Stop, think, act. Some teach it as, Stop, breathe, think, act. This is wonderful, but it doesn't explain why and while the concept is a good one, a better understanding of the panic cycle is essential to breaking it. If a diver feels himself starting to hypoventilate, he needs to know this is an important step that can lead to his (and his buddy's) death if he doesn't take immediate action by slowing his breathing. Slow deep breaths are a life saver.