Babies rock back and forth to soothe themselves according to Beth Morrisey of Kids' Behavior. A baby will often rock rhythmically to help herself fall asleep.
Babies may rock back and forth for several reasons. They may be using it as a means to express their frustration or as a type of pain relief. Some children rock themselves all of the time at bedtime, while others do not routinely rock and begin to do so when they are experiencing pain or some form of stress. A child may be singing or humming in his or her head and rocking at the same time. Rocking back and forth can sometimes be an indication of an autism spectrum condition.