During the Tang Dynasty, Emperor Li Shimin, in order to conquer the Turkic peoples in the north and pacify their repeated encroachments, ordered his general Li Jing to personally lead his troops on a campaign to fight on the borders, which resulted in repeated successes. He returned to the capital in triumph on August 15th. In order to celebrate the victory, inside and outside the city of Chang'an, the army and the people celebrated the victory all night long. At that time, there was a Tubo who came to Chang'an to do business, specially to the emperor to offer round cake to celebrate the victory.
Tai Zong Li Shimin was so happy that he took the ornately decorated cake box, took out the colorful cakes, and pointed to the bright moon hanging in the sky and said, "I should invite the toad (i.e., the moon) to the Hu cake." Afterwards, the cakes were distributed to the civil and military officials. Since then, the custom of eating mooncakes on the Mid-Autumn Festival has been passed down.
Expanded Information:
Origin of the Mid-Autumn Moon Festival Mooncake:
According to the legend of China's ancient times. Emperors had the ritual of sacrificing the sun in the spring and the moon in the fall. In folklore, every August Mid-Autumn Festival, there are also around the moon worship or moon sacrifice custom. "The moon is full on the fifteenth day of the eighth month, and the mooncakes are fragrant and sweet," says the famous proverb, which describes the custom of eating mooncakes in urban and rural areas on the night of the Mid-Autumn Festival.
Mooncakes were first used as offerings to the moon god, and then people gradually took the Mid-Autumn Moon Festival and tasted mooncakes as a symbol of family reunion, and slowly mooncakes became a gift for the festival. Mooncakes were originally used by the Tang Dynasty to celebrate the victory of the army. During the reign of Emperor Gaozu of the Tang Dynasty, General Li Jing was victorious in his conquest of the Xiongnu, and returned home in triumph on the 15th day of the 8th month.
The Turpanese, who were merchants at the time, offered mooncakes to the Tang emperor. Li Yuan, the first emperor of the Tang Dynasty, took the gorgeous cake box, took out the round cake, and pointed to the bright moon in the sky with a smile and said, "I should invite the toad with the Hu cake." After saying this, he distributed the cakes to his ministers to eat together.
Baidu Wikipedia-Mooncakes