It all has to do with intermolecular forces.
The hotter something is, the more energy each molecule (or atom, as the case may be) has.
In water, at cooler temperatures (below 273K), the molecules have a small enough amount of energy that hydrogen-bonds between molecules stay relatively fixed. The molecules don't have enough energy to break out of the hydrogen bonds, so they stay where they are. In water, these hydrogen bonds lead to a crystal structure that takes up lots of space.
As you heat the water (between 273K and 277K), more and more molecules are able to break free from the hydrogen bonds that once trapped them. This lets the structure collapse a little, leading to shrinkage.
(Note: I've ignored the liquid vs solid part here, but the premise remains the same. Liquid water at 273K allows for more intermolecular hydrogen bonding than liquid water at 277K)