A pencil partly immersed in water in a glass tumbler appears to be displaced at the interface of air and water because:
Light does not travel in the same direction in all media
When light rays travel from one medium to another, its direction changes because of the difference in refractive indices.
A ray of light coming from the lower end X of the pencil travels from water to the air and gets refracted away from normal as it travels from denser to rarer medium. Another ray coming from X will also get refracted in the same way. The two refracted rays, when produced backward, appear to meet at point Y as shown in the figure. Thus, a virtual image is formed at Y.
Hence, the pencil is actually straight but on immersing in water, it appears to be displaced at the interface of air and water.