They have a flexible neck because they have more neck vertebrae than many other vertebrates. The greater number of vertebrae means there are more joints that allow each vertebrae to move with respect to the next vertebrae. It is like having a chain with more links. Each link has only limited flexibility so more links give the chain or neck more flexibility.
Frogs, for example, have a very inflexible neck because they have just one neck vertebra. Mammals have a more flexible neck than frogs because most of them have 7 neck vertebrae. Even the long neck of the giraffe is made up of only 7 neck vertebrae. So, although the giraffe has a longer neck then we do, it is not more flexible than ours. Most mammals have 7 neck vertebrae because practically all living mammals evolved from shrew-like insectivores that lived underground and survived the giant meteor that struck the earth and wiped out the dinosaurs. These small mammals did not need a long or flexible neck, so they have far fewer vertebrae than birds.
Birds have had their hands tied up with wings for more than 160 million years. They use their mouths to do many things that some animals do with their hands. Therefore they can certainly use a long and flexible neck to help them reach many parts of their body. Birds preen their feathers frequently and a flexible neck would be very helpful. The earliest known bird, Archaeopteryx, has 10-11 neck or cervical vertebrae. That gives its neck more flexibility than the necks of most mammals. Given the usefulness of a flexible neck to birds, it would be unlikely that they will lose that flexibility through evolution by evolving fewer neck vertebrae.