Stomach pain in the middle of the night is mainly due to excitation of the vagus nerve (a nerve that controls digestion in the human gastrointestinal tract and can control the secretion of gastric juices), which leads to secretion of gastric acid and stimulates duodenal ulcers, causing stomach pain.
Therefore, stomach pain at night and stomach pain before meals (on an empty stomach) is usually suspected of having a duodenal ulcer.