We generally don’t think of muscles as a cause of pain. Sure, we know that after exercise or playing ball we can have muscle soreness, but when pain persists we often think it is coming from nerves, joints or the spine. Believe it or not, muscles (and other soft tissue) are the most common reason for pains such as lower back pain, neck pain and shoulder pain. The brain gets information from the body about pain from specialized nerves called nociceptors that respond to tissue damage. Trauma, overwork, and over-exercising cause low oxygen and too much acidity in the muscle. These changes, along with other chemicals that are produced when the muscle is damaged in any way, stimulate the nociceptor. When a stimulus as strong enough it causes the nerve cell to produce an electrical impulse that is sent into the spinal cord and then up to the cortex, the part of the brain where we perceive pain.
There are actually more nociceptors in the muscle attachment sites (the ends of the muscle where it attaches to the tendon and the tendon attaches to the bone) than in the muscle tissue. That is why if you have pain originating in muscles you may be more aware of the pain close to a bone than in the middle of the muscle.
Any kind of injury releases substances from damaged muscle and surrounding tissue that stimulates the nociceptor. If the nociceptor gets enough stimulation it creates an electrical discharge which travels down the nerve and ends up in the spinal cord. Muscle nerves that have been stimulated repeatedly become more sensitive to additional stimulation. They are called sensitized nerves and they will more easily produce electrical activity with even non painful events such as any contraction to move the muscle. That is why when an injured muscle is used it may cause pain. That is why if you have strained muscles for any reason you may feel pain from every day movement. Nociceptors are key structures in the perception of pain.