Last updated: 05-16-2018
After escaping from Laozi’s furnace, Sun Wukong battles his way through heaven until the Buddha is called in to halt his rebellion. The Enlightened One makes him a wager that, if he can jump out of his hand, the macaque will become the new ruler of heaven. Monkey agrees to the wager and jumps into his palm. With one tremendous leap, Sun speeds towards the reaches of heaven, clouds whizzing by him in a blur of colors as he travels across the sky. He lands before five great pillars, thinking them to be the edge of the cosmos. He tags one of the pillars with his name and urinates at the base of another in order to prove that he had been there. Upon returning, he demands that the Buddha live up to his end of the bargain. Yet the Buddha explains that he had used his infinite powers to cloud Sun’s mind, tricking him into thinking he had left, when he actually stayed in his hand the entire time. But before Monkey can do anything, the Buddha overturns his hand, pushing it out the gates of heaven, and slamming it onto earth, transforming it into the Five Elements Mountain (Wuxing shan, 五行山). There, Sun is imprisoned for his crimes against heaven (Wu & Yu, 2012, pp. 193-195).
I. The source
The idea of a primate demon being imprisoned under a mountain can be traced to a story appearing in Tang (618-907) and Song Dynasty (960-1279) sources. The first and shortest version appears in the Supplement to the History of the Empire (Guoshi, 國史補, early 9th-cent.):
There was a fisherman at Chuzhao 楚州 who unexpectedly hooked an ancient iron chain in the Huai [river 淮河], but could not pull it clear. He reported this to the Prefect, Li Yang 李陽, who summoned a large number of men to draw it out. At the end of the chain was a black monkey; it jumped out of the water, then plunged back and vanished. Later this was verified in the Shanhai jing 山海經, from the words—’A river-beast persistently wrought destruction.  Yu [the great 大禹] chained it below Junshan 軍山. It’s name was Wuzhiqi’ 無支奇 (Andersen, 2001, pp. 15-16). 
A modern depiction of Wuzhiqi (larger version).
A longer and more well-known account appears in Extensive Records of the Taiping Reign (Taiping guangji, 太平廣記, 978). The entry explains how the water spirit Wuzhiqi came to wear the iron chain. The passage is quite long, so I’ll paraphrase the beginning and middle sections:
[While taking a boat (c. 797), Li Gongzuo 李公佐 of Longxi 隴西 by chance meets Yang Heng 楊衡 of Hongnong 弘農. Yang tells Li the following story.] 
In the reign period of Yongtai (765) Li Tang was governor of Chuzhou. One night, at that time, a fisherman was out fishing below Turtle Mountain [龜山], when his hook caught on something and would not come up again. The fisherman was an expert swimmer and quickly went down to a depth of fifty zhang (c. 150 meters), where he saw a great iron chain encircling the base of the mountain. He searched but could not find the end of it, and he subsequently informed Li Tang about it.
Li Tang ordered several tens of fishermen and people who could swim to get hold of the chain, but they were unable to pull it free. He increased their forces with that of fifty heads of oxen, and now the chain gave way and began little by little to move onto the shore. There was no wind at the time, but all of a sudden the waves started to roll and gush forth, and those who watched were greatly frightened. At the end of the chain a beast appeared, shaped like a monkey, with [a] white head and long mane, teeth like snow and golden claws, and it rushed onto the shore. It was more than five zhang high (c. 15 meters), and it squatted in the manner of a monkey; only it could not open its eyes, but sat motionless as if in a daze. Water ran down from its eyes and nose in a stream, and its spittle was so repellent and foul-smelling that people could not be near it. After a long while it stretched out its neck and lowered it, and suddenly opened its eyes. They were as bright as lightning, and it looked at people around it, raging with madness. Those who watched took to their heels, and the beast very slowly pulled at the chain and dragged the oxen with it into the water, never to emerge again. Many knowledgeable and prominent men from Chu were present, and they looked at Li Tang and at each other, startled and fearful, not knowing the source of this. Since then the fishermen were aware of the location of the chain, but in fact the beast never appeared again.
[Li travels to Mt. Bao (Baoshan, 包山) and, with the help of the Daoist Zhou Jiaojun 周焦君, deciphers the following account found in the eighth scroll of the Classic of Peaks and Rivers (Yuedu jing, 岳瀆經).] 
When Yu regulated the waters, he went thrice to the Tongbai 桐栢 mountains, and each time a storm broke, with crashes of thunder, shattering the stones and making the trees groan. The Five Earls, Wubo 伍伯, restrained the waters, the Celestial Elder, Tianlao 天老, harnessed his troops, but they could not begin their work. In anger Yu summoned the hundred sacred powers. He searched for and called upon the Dragon Kui, Kuilong 夔龍, and the thousand spiritual lords and seniors of Mt. Tongbai bowed their heads and asked for his command. Yu then assigned Hongmeng 鴻蒙氏, Zhangshang 章商氏, Doulu 兜盧氏, and Lilou 犁婁氏, to the task, and they captured the god of the rivers Huai and Guo, whose name was Wuzhiqi 無支祁. It answered readily when spoken to and knew about the shallow and the deep parts of the Yangzi and the Huai, and about how far the marshlands extended. It was shaped like a monkey, with [a] flattened nose and high brows, black body and white head, metallic eyes and teeth like snow. Its neck stretched out to a length of a hundred feet (c. 30 meters), and its strength exceeded that of nine elephants. It attacked in high leaps and running swiftly, its movements were agile and sudden, and one could not keep it in sight or hearing for very long.
Yu turned it over to Zhanglu 章律, but he could not control it. He turned it over to Niaomuyou 鳥木由, but he could not control it. He turned it over to Gengchen 庚辰, and he could control it. For thousands of years the Chipi 鴟脾 and the Huanhu 桓胡, the tree goblins and the water spirits, the mountain sprites and the stone prodigies had roamed and roared and gathered around it, but Gengchen used his military prowess to chase them away. He tied a great chain around its neck, pierced its nose and attached a golden bell to it. He placed it under the base of Turtle Mountain at the southern bank of the Huai, so that forever after the Huai flowed peacefully into the sea. Since the time of Gengchen people would all make images of this shape in order not to suffer from the waves and rainstorms of the Huai (Andersen, 2001, pp. 17-21). 
This entry presents Wuzhiqi as a supernaturally strong monkey demon with flashing eyes and the ability to perform transformations (by stretching its neck). The gods then imprison him below a mountain to punish his affront to the natural order (calm the waves created by him). It’s no wonder then how it influenced the final novel.
Another source comes from the early Ming (14th-15th-cent.) zaju play Journey to the West (Xiyouji, 西遊記) by Yang Jingxian (杨景賢). But instead of it being the Buddha, the monkey is trapped by the Bodhisattva Guanyin, and instead of Five Elements Mountain, it is his home of Flower Fruit Mountain. Dudbridge (1970) paraphrases scene nine, titled “The Holy Buddha Defeats Sun”:
Sun Xingzhe [孫行者, Pilgrim Sun] now appears: after an initial poem vaunting his celestial birth, his ubiquity and power, he lists out the members of his ape family, alludes to his career of misdeeds and his wife, the abducted Princess of Jindingguo [金鼎國, the Golden Cauldron Country].
Devaraja Li appears, with orders to recover the possessions stolen by Sun from the Queen Mother of the West. He issues orders to his son Nezha, who enters with troops upon orders from the Jade Emperor to capture Sun Xingzhe in his home Ziyun luo dong [紫雲羅洞, Purple Cloud Cave] on Huaguoshan.
The princess-wife now enters (the singer in this act) and tells in song the story of her abduction and the life on this mountain. She is joined by Sun and they prepare to feast.
The celestial troops surround them, Sun’s animal guards flee and Sun himself escapes. Devaraja Li ‘combs the hills’ and meanwhile finds the princess, who now sings through the remainder of her suite of songs until it is decided to give her escort back to her home.
Sun Xingzhe eludes the forces of Nezha and is captured only by intervention by Guanyin, who has him imprisoned beneath Huaguoshan to await the arrival of Tripitaka, his future master (p. 195). 
1) Anderson (2001) explains that no such entry for Wuzhiqi exists in this historical bestiary (p. 16).
2) The demon Wuzhiqi is presented as a sister to Sun Wukong in a Yuan-Ming era stage play.
3) Li Gongzuo (c. 770-c. 848) was a historical story teller and the presumed author of this particular tale (Andersen, 2001, p. 16).
4) This is most likely a fictional version of the aforementioned Shanhai jing (Andersen, 2001, p. 16).
5) One such statue dating to the Song Dynasty is held in the Museum fur Ostasiatische kunst in Berlin. It is discussed at length in Anderson (2001).
6) Source slightly altered. The Wade-Giles was changed to Pinyin.
Andersen, P. (2001). The demon chained under Turtle Mountain: The history and mythology of Chinese river spirit Wuzhiqi. Berlin: G-und-H-Verl.
Dudbridge, G. (1970). The Hsi-yu chi: A study of antecedents to the sixteenth-century Chinese novel. Cambridge: Cambridge Univ. Press.
Wu, C., & Yu, A. C. (2012). The journey to the West: Volume 1. Chicago, Illinois : University of Chicago Press.