Has a lot to do with first, mindfulness. Basically becoming aware of physical, mental, or emotional red flags. If you can notice the little warnings that something ugly is coming you stand a chance of learning to stop it before it starts. That is where I started at least. Now, this has changed for me over the years. Used to I would get a weird feeling as if I were watching my surroundings through unfamiliar eyes. It's the best way to describe it.
The past few years has been more physical , such as I will become highly aware of maybe my heart beat, or my breathing. While everything will be normal, for whatever reason the strange awareness causes me to think they are not, then I panic. I actually was unable to move my thumbs a few days ago while panicking for this reason.
Anyhoo, the mindfulness part is essential. Step one really. Then you have to remember to try and bring yourself down. That was a hard thing. environmental cues help. Maybe a special ring, bracelet, or necklace. Or maybe little things placed around the house that make you remember your intent to try to calm yourself. I made meditation beads which are usually in plain sight, or a little book of meditations, anything really that brings your attention to the fact that you want to try and reel it in.
Now how do you actually regain control? This can be very different for everyone. For me literally talking to myself works well. As you may predict a deep breath followed with things like: you know you're okay, everything is fine, you are doing this to yourself, calm down. Or "I" statements instead of "you're" statements. I am fine, I am in control, I am stopping this before it goes any further.
I don't like to use words like panic, fear, afraid, etc.. I think the word choice matters. So I choose words like calm, relaxed, okay, fine, in control. The mental impact feels different.
Other people go to deep breathing or some physical movement. I tend to cover my face with my hands while taking a very slow and deep breath, and on the exhale is when I do the talk down.
Just a little of what I personally have practiced. In the moment it takes a lot to turn it around, but it can be done with practice. It's like taking yourself to the gym. You won't get a 6 pack in a day, but with regular effort you will see positive results. I had some links to helpful articles on this. If I can dig them up I will post them for you.