Neural circuits work together with muscles and sensory feedback to generate motor behaviors appropriate to an animal's environment. In C. elegans, forward locomotion consists of dorsoventral undulations that propagate from anterior to posterior. How the worm's motor circuit generates these undulations and modulates them based on external loading is largely unclear. To address this question, we performed quantitative behavioral analysis of C. elegans during free movement and during transient optogenetic muscle inhibition. Undulatory movements in the head were found to be highly asymmetric, with bending toward the ventral or dorsal directions occurring slower than straightening toward a straight posture during the locomotory cycle. Phase shifts induced by brief optogenetic inhibition of head muscles showed a sawtooth-shaped dependence on phase of inhibition. We developed a computational model based on proprioceptive postural thresholds that switch the active moment of body wall muscles. We show that our model, a type of relaxation oscillator, is in quantitative agreement with data from free movement, phase responses, and previous results for frequency and amplitude dependence on the viscosity of the external medium. Our results suggest a neuromuscular mechanism that enables C. elegans to coordinate rhythmic motor patterns within a compact circuit.