The twinkling of stars occurs due to the atmospheric refraction of starlight. As starlight enters the Earth's atmosphere, it passes through layers of air of varying density. This causes the light to bend or refract multiple times before it reaches the observer’s eyes. These changes in direction cause stars to appear as if they are shifting position or twinkling.