Check out this video of a mechanical engineer from Alabama explaining and demonstrating this ability with a family chicken!
Birds can do this because they have extremely long necks that extend really far and are usually shaped like an "S". This allows them to keep their heads still while their body moves around and gives them the ability to "bob" their heads when they walk. Even birds that don't appear to have necks at all have pretty long necks, but the whole thing is covered by feathers. This is all to keep their field of vision steady for a longer period of time. It aids in their ability to detect movement, which allows them to find tiny moving prey, and lets them know when something (like a predator) is approaching.