In the beginning you should pick a pen/pencil that is relatively smooth and evenly weighted. Once you advance you will be able to pick up almost any pen and spin it!
Find the center of balance of the pen or pencil you chose. Most pens and pencils won't balance exactly in the center, as long as you can balance it on the tip of your finger you're ok
Your initial grip is the most important. Grab the item approx one finger length away from the center (keep the heavier end away from your hand) with your thumb, index and middle finger
Your middle finger should preload pressure on the item, and that pressure is released by opening up your index finger which, because your finger is slightly off center, spins the pen around your thumb
YOUR THUMB SHOULD BARELY MOVE
Notice the thumb doesn't move, the index finger extends and the middle fingers built up pressure is released, which spins the pen around the thumb
If your doing it right, the pen will almost stick to your hand like a magnet and spin over the back of your thumb. Keep your index finger extended so the pen doesn't hit it
Once the pen gets by your index finger, clamp down with your thumb and index finger. Practice makes perfect, this takes a little bit to learn where to place the pen and how the "magnet" feeling works