I figured out that my neck gets agitated after bending down when working or using improper form in physical activities. SO the first step in prevention is to be careful with your neck before the problem arises.
Regardless, if your neck is killing you right now, here is what I would do. If you don't have a foam roller I would seriously consider purchasing one as they are not only great for breaking the tension in the upper back, but can be used on the quads and calves after a long walk or run. If you don't have a roller handy at the moment, then grab a tennis ball or golf ball, place it in a stocking and find a bare wall. From there place the ball on your back wherever there is tension and hold it there or slowly roll it back and forth trying to get rid of the knot. Although painful, this almost immediately eases my head aches. Sometimes the root of my headaches come from my lower back, which is why a foam roller is so helpful. Because of its length you can get the entire area of your back and then use the golf ball as spot work for releasing tension. Nothing helps more than these couple of tools.
If these aren't around your house, grab some ice and ice the region for a bit. As we all should know, ice helps decrease inflammation tremendously. If you are still having neck pain, use your fingers and gently massage the points that are sore (I emphasize gently because you don't want to put your neck into spasm).
Lastly, if this seems to be a persistent problem that doesn't pop up because of over-work or misuse then maybe you should consider seeing an acupuncturist, going to yoga, or changing your pillow.
Hope all of this helps with your neck pain! What else do you do for your neck?