The twinkling of stars is caused by atmospheric refraction. Light from a star travels through different layers of the Earth's atmosphere, which have varying optical densities. As a result, the light undergoes continuous bending, making the star's position appear to shift slightly. This fluctuation in brightness creates the twinkling effect.