Having a stiff neck that just happens all of a sudden could be simply caused by muscle strain to the neck muscles or even soft tissue sprain.
A stiff neck can also sometimes be caused by the way you're sleeping and have your head positioned that strains the neck muscles.
If that's not the problem and the stiff neck continues and doesn't go away within the same day then I would get checked out by a doctor or go to urgent care for the stiff neck.
In some cases a stiff neck that happens all of a sudden can be caused by Meningitis.
Meningitis can be a very serious condition that causes inflammation of the brain and spinal cord membranes that is caused by an infection.
Meningitis can be fatal if not treated so get checked out to make sure it's not meningitis because if you do get the meningitis treated it can be cured.