Due to a lack of humidity in the local environment, your cat's fur builds up quite the electric charge. Then, when you pet it and add friction to the mix, both you and your cat feel a shock.
This is what every cat owner wants to know after feeling the shock because cats usually have a very striking reaction to it. However, the level of pain experienced by the cat in such conditions is not an alarming one.
The best possible way to fix this issue is to raise the humidity levels in your home. This should reduce the level of static electricity at first and then, get rid of it entirely.
Similar to using sprays on your cat, getting your hands wet before touching your cat could spare you the shock, but it will leave your cat's fur exposed to tangling, especially if done frequently.
It all depends on your cat. Always use products that are a good fit for your cat's breed and keep in mind that the ultimate goal is to moisturize the fur.