The speed of light differs in each medium, warping the image.
This phenomena is known as refraction. It is the bending of a wave when it enters a medium where its speed is different.
Now we know that the pencil doesn't actually warp when it enters the water (the second medium - first is air), however, the speed of light differs in each medium.
Light in air is much faster than in water -
If the speed of light in the first medium is faster than in the second medium, light bends towards the medium (the boundary - in this case, the boundary between air and water).
If the speed of light in the first medium is slower than in the second medium, light bends away from the medium. An example of this would be if light from underwater is directed to the sky.
Hope this helps :)