While everyone is giving you good advice about how to be yourself and how to deal with jerks, I am going to take a different approach and simply answer your question.
In order to have wit you have to have two things: an understanding of humor and thick skin.
Understanding humor means being able to break apart the message and find a way to reinterpret it in a relevant but unexpected way. The surprise is the key to a good joke, but it has to make sense. Humor exposes a truth, thus, an intentionally funny person must be able to see the truth in the world and in themselves.
Humor is a tool that takes the form of what you need. It can be a weapon, to insult or make foolish, a shield that repels, a cloak to hide your feelings or intentions, a clown nose to make another laugh, and so on.
The best way to learn humor is by listening to funny people. Watch a lot of stand-up comedians. Not only will it make your day brighter but you will absorb the essence of humor. Watch comedians that work the crowd. See how they get people to interact with them, how they handle hecklers. Even when they are under extreme pressure by performing in front of an audience, they always seem to be in control of themselves. That's how you need to be.
The second part is just as important as the first. You have to have thick skin. You can't take things personally. Humor and wit comes from a special mental place. Being too angry or hurt will prevent you from accessing this type of witty thinking. It requires a bit of cool confidence, in that even if everything else about you is lousy, you know you have the power of communication to protect or devastate at your fingertips.
To be able to clap back at someone quickly means brushing off what they say, analyzing the statement to find how to turn it around in an unexpected way, and then deliver it, not with hot anger, but with a cool, calculated malice. Slip the verbal knife between their ribs with a practiced ease and a smirk.
Do this enough and you'll get faster and faster. Pretty soon you're not even thinking on a conscious level. It only takes milliseconds to react, like a gunslinger.
Match the level. An eye for an eye. If someone is giving you a good natured poke, you can play along with them and take the 'abuse' in a funny way, or you can zing one back at them. Or even deflect it with a humorous meta comment.
If they are testing you, increase the pressure.
If they are mean spirited, go for the jugular.
You have to keep your feelings in check to respond at an appropriate level.
Being funny is a skill. But it's one that is hard to learn by thinking about it. You see it in action and you have to practice it.
Read famous insults by kings, stars, world leaders, etc. A lot of public facing people were very funny when the time was right, and had to be because they faced constant criticism.
Read classic humorous essays. Watch entertaining people tell a story. Try some jokes out in the mirror. Make a funny observation about something or someone to a friend. Don't be afraid to look foolish, once you've embarrassed yourself enough trying to make people laugh, no insult in the world can affect you.
Hope this helps.
Source: Me, a resplendent fool
