The cremaster muscle is a muscle that controls the height at which the testes hang in human males. In females it is found near the uterus, but is underdeveloped.

The cremaster is attached in quite an unconventional way. It is secured at one end to the inguinal ligament within the groin. It passes out of the abdominal cavity with the vas deferens, and then wraps around both this and the appropriate testis. Contraction of the cremaster muscle brings the testes closer to the body.

The nerve that controls the cremaster muscle branches off from the genitofemoral nerve. The cremaster muscle is not usually under conscious control. The cremaster muscle contracts in cold conditions to bring the testes closer to the body, and relaxes in hot weather. This is due to the need for the testes to be temperature regulated, to ensure optimal conditions for sperm production. Contraction can also be caused by the cremasteric reflex. Stroking the inside of the thigh causes the cremaster to contract. This may occur because there are many nerve endings there that also come from the genitofemoral nerve.

In some cases, strong contraction of the cremaster may cause the testes to be drawn up out of the scrotum and into the body, which can be exceedingly painful. This is what occurs in pseudocryptorchidism. This can occur either due to stress, excessive cold or other factors. An oversensitive cremaster may be a contrary indication to working in a refrigerated factory.

Sources:
Gray's Anatomy
abc.net.au
Arkansas University
Collins Medical Dictionary