How can I cure back and neck pain caused by sleeping in a bad position?

Heat exercise massag. Moist heat applied to the neck & shoulders; pain med (aleve/advil/motrin—if tolerated by stomach & not on blood thinner coumadin); magnesium to help relax muscles (caution: too much may cause loose bm's); neck and shoulder exercises to relax and fatigue cramped muscles; therapeutic massage; consult doctor if problem worsens or persists more than a few days.
Change positions. You might want to experiment with changing the position that you sleep in. You may also want to experiment with different pillows. Pain from sleeping in an abnormal position will usually improve on its own, but you will need to visit your physician if you continue to have pain greater than 2 weeks.