Nobody would do this with practical effects these days. it takes 5 minutes to model a pencil in a 3D program and then another 10 minutes to integrate it into a 3D tracked plate. Otherwise you can always use magnets in the pencil(s). You know, 6th grade physics lessons from school...
You could do it without modeling a pencil by first shooting a clean plate, then filming the pencil or pencils facing north, then shoot the pencils facing south. If the pencils are in a cup so that part of the pencils are hidden you'll also need to shoot a shot of each pencil separately that you can line up with the pencils in the cup layer. Let's assume that you just have the pencils on a desktop.
Here's what you do. Bottom layer is the clean plate, the desktop only. Next layer is the pencils facing north, top layer is the pencils facing south. Now mask the pencils on the top two layers so you only have pencils. You'll need a separate mask and separate layer for each pencil you want to flip. Now you just trim the top two clips so that the pencils start the shot facing north, then a few frames layer, the pencils are facing south. Now you move the anchor point for the first pencil you want to flip to the place where you want to rotate the first pencil, add position and rotation keyframes and then rotate and move the pencil half way through the rotation, then you go to the copy of the pencil that is facing south, move the anchor point to the center of rotation, then move down the timeline a few frames giving the pencil a chance to come to rest and set position and rotation keyframes by pressing Alt/Option + p and Alt/Option + r. Now move back to the cut point and rotate and position the masked pencil so the it matches the position and rotation of the first pencil. Throw in some motion blur for the move and you're done. The way to sell this effect is to make the motion quick enough that motion blur hides the fact that you've done the masking trick. The other trick to pulling this off is to have the camera on a tripod so that it doesn't move. You can still do it with camera movement but it's going to be a lot more work.