The first recorded history of Dunhuang, a vital station along the legendary Silk Road, was related by Emperor Wu of the Han Dynasty (206 B.C. - 220). However, modern archaeological findings trace the region's history as far back as the Xia Dynasty (21st - 16th century B.C.). During the Shang and Zhou Dynasties over the next 1,400 years, the area was inhabited by three nomadic tribes-the Qiang, Wusun and Yuezhi (Rouzhi). At the end of the Qin Dynasty (221 B.C. - 206 B.C.), the Yuezhi conquered the other two tribes and occupied the entire Hexi Corridor. This corridor (also called Gansu Corridor) was the primary segment of the ancient Silk Road. Later, the Hun (or Xiongnu) armies vanquished the Yuezhi and established dominance here during the early Han Dynasty. From the early seventh century B.C., Chinese kingdoms built walls along their northern frontiers to defend themselves against the warlike Huns. During the Han Dynasty, the most ambitious Great Wall project to date was begun with four systems of fortification spanning from Dunhuang in the west all the way to the Korean peninsula in the east.
Dunhuang's importance as a military fortress led to its becoming a prefecture in 177 B.C. concurrently, construction of the Yumenguan Pass and Yangguan Pass to the west of Dunhuang opened a new trade route between the central plains (Hexi) and the states in Xi Yu (literally the western regions, referring to most parts of Xinjiang and part of central Asia) namely the noted Silk Road. As more and more immigrants from the central plains settled here, Dunhuang gradually became a prosperous agricultural base as well as the key military installation on the Han's border frontier. For the next several centuries, Dunhuang served as one of China's most important military, political and economic centers.
Commercial development in the Han Dynasty was soon accompanied by the growth of religion. The arrival of Confucianism initially and later Taoism and Buddhism would play an important role in Dunhuang's development. During this period, however, political turbulence prevailed as five different regimes rules the region. At this time Buddhism began to gain a strong foothold and its earliest grottoes were built in the nearby Magao Caves. In the ensuing years, the Silk Road flourished as the popularity of Buddhism increased. More Buddhist writings and music were brought to Dunhuang with the encouragement of the Sui (561 - 618) and Tang (618 - 907) Dynasties, more and more local people began to embrace Buddhism.
The Tang continued its dominance of the region fending off military attacks including a defeat of the Tufan (the ancestors of Tibetans). Dunhuang and the Hexi Corridor would remain vital military fortresses. During this time, the art, ideas and cultures of other civilizations such as Greece, India, and central and western Asia were imported into Dunhuang.Eminent Buddhist monks continued to come here to carry forward their dharma. The Magao Caves, the greatest example of Buddhist cultural significance, continued to house religious statuary and frescoes in its grottoes.
In the Southern Song Dynasty (1127 - 1279), warfare in the region became more frequent and commerce was gradually shifting from land to sea routes. This resulted in Dunhuang gradually losing its national stature as the economic gateway to the western regions. During the Ming Dynasty (1368 - 1644) nomadic Mongolian tribes occupied the region. After the Mongolian occupation, Islamic tribes conquered the region in 1516. This event marked the end of the centuries-long presence of Buddhism in Dunhuang. Much Buddhist art and religious relics destroyed although some reconstructed works were salvaged in the Qing Dynasty (1644 - 1911). While Dunhuang never regained its commercial and military status, it does remain a premier tourist destination largely because of the peerless Magao Caves.