A lot of people have an experience of waking up in the morning with a niggle in the back of the neck, which gets worse as they stretch out or stay still for long. Usually happens to females when they don't dry their hair before sleep and sleeps in an awkward position to avoid the wet hair.
Our spines have something called the 'facet joint' at the back of it. The function of these joints is to guide motion in each vertebral joints. Most joints are made out of ligament on the outermost part of it.
When we sleep with awkward neck positions, these joint ligaments lengthen overtime, and become damaged. When damaged, it will cause inflammation to remove the dead particles and restore the tissue. You've basically sprained your neck ligament, or got torticollis.
Although the symptoms may resolve overtime, it's recommended that you see a chiropractor or any other practitioners to rule out serious injuries such as neck disc injuries.
The symptoms tends to get worse with prolonged position because the other joints stiffen, and also stretching because it will stretch the over-stretched joint even more.