Many things can cause your neck to hurt, such as poor posture, bad sleep habits, sports injuries, infections, tumors, arthritis, repetitive motion and so on. Maybe you can try to think it over whether the condition is likely caused by these factors. For example, whether you have experienced any injury recently or whether you have a good sleep habit. By ruling out some factors, you may find out what exactly lead to the pain.
Don't worry about too much if you can't do this. You can ask your doctor for help.