Evidence of the earliest established society other than the prehistoric Iron Age Đông Sơn culture in Northern Vietnam was found in Cổ Loa, an ancient city situated near present-day Hà Nội.
According to myth, the first Vietnamese people were descended from the Dragon Lord Lạc Long Quân and the Immortal Fairy Âu Cơ. Lạc Long Quân and Âu Cơ had 100 sons before deciding to part ways. 50 of the children went with their mother to the mountains, and the other 50 went with their father to the sea. The eldest son became the first in a line of early Vietnamese kings, collectively known as the Hùng kings (Hùng Vương or the Hồng Bàng Dynasty). The Hùng kings called their country, located on the Red River delta in present-day northern Vietnam, Văn Lang. The people of Văn Lang were known as the Lạc Việt.
Song Da bronze drum's surface, Vietnam Văn Lang is thought to have been a matriarchal society, similar to many other matriarchal societies common in Southeast Asia and in the Pacific islands at the time. Various archaeological sites in northern Vietnam, such as Đông Sơn have yielded metal weapons and tools from this age. Most famous of these artifacts are large bronze drums, probably made for ceremonial purposes, with sophisticated engravings on the surface, depicting life scenes with warriors, boats, houses, birds and animals in concentric circles around a radiating sun at the center.
Many legends from this period offer a glimpse into the life of the people. The Legend of the Rice Cakes is about a prince who won a culinary contest; he then wins the throne because his creations, the rice cakes, reflect his deep understanding of the land's vital economy: rice farming. The Legend of Giong about a youth going to war to save the country, wearing iron armor, riding an armored horse, and wielding an iron staff, showed that metalworking was sophisticated. The Legend of the Magic Crossbow, about a crossbow that can deliver thousands of arrows, showed extensive use of archery in warfare.
By the 3rd century BC, another Viet group, the Âu Việt, emigrated from present-day southern China to the Red River delta and mixed with the indigenous Van Lang population. In 258 BC, a new kingdom, Âu Lạc, emerged as the union of the Âu Việt and the Lạc Việt, with Thục Phán proclaiming himself "King An Dương Vương". At his capital Cổ Loa, he built many concentric walls around the city for defensive purposes. These walls, together with skilled Âu Lạc archers, kept the capital safe from invaders for a while. However, it also gave rise to the first recorded case of espionage in Vietnamese history, resulting in the downfall of king An Dương Vương.
In 207 BC, an ambitious Chinese warlord named Triệu Đà (Chinese: Zhao Tuo) defeated king An Dương Vương by having his son Trọng Thủy (Chinese: Zhong Shi) act as a spy after marrying An Dương Vương's daughter. Triệu Đà annexed Âu Lạc into his domain located in present-day Guangdong, southern China, then proclaimed himself king of a new independent kingdom, Nam Việt (Chinese: 南越, Nan Yue). Trọng Thủy, the supposed crown prince, drowned himself in Cổ Loa out of remorse for the death of his wife in the war.
Some Vietnamese consider Triệu's rule a period of Chinese domination, since Triệu Đà was a former Qin general. Others consider it an era of Việt independence as the Triệu family in Nam Việt were assimilated to local culture. They ruled independently of what then constituted China's (Han Dynasty). At one point, Triệu Đà even declared himself Emperor, equal to the Chinese Han Emperor in the north.
In 111 BC, Chinese troops invaded Nam Việt and established new territories, dividing Vietnam into Giao Chỉ (Chinese: 交趾 pinyin: Jiaozhi, now the Red river delta); Cửu Chân from modern-day Thanh Hoá to Hà Tĩnh; and Nhật Nam, from modern-day Quảng Bình to Huế. While the Chinese were governors and top officials, the original Vietnamese nobles (Lạc Hầu, Lạc Tướng) still managed some highlands.
In 40 AD, a successful revolt against harsh rule by Han Governor Tô Định (蘇定 pinyin: Sū Dìng), led by the two noble women Trưng Trắc and her sister Trưng Nhị, recaptured 65 states (include modern Guangxi), and Trưng Trắc became the Queen (Trưng Nữ Vương). In 42 AD, Emperor Guangwu of Han sent his famous general Mã Viện (Chinese: Ma Yuan) to quell the revolt. After a torturous campaign, Ma Yuan defeated the Trưng Queen, who committed suicide. To this day, the Trưng Sisters are revered in Vietnam as the national symbol of Vietnamese women. Learning a lesson from the Trưng revolt, the Han and other successful Chinese dynasties took measures to eliminate the power of the Vietnamese nobles. The Vietnamese elites would be coerced to assimilate into Chinese culture and politics. However, in 225 AD, another woman, Triệu Thị Trinh, popularly known as Lady Triệu (Bà Triệu), led another revolt which lasted until 248 AD.
During the Tang dynasty, Vietnam was called Annam (Giao Châu), until the early 10th century AD. Giao Chỉ (with its capital around modern Bắc Ninh province) became a flourishing trading outpost receiving goods from the southern seas. The "History of Later Han" (Hậu Hán Thư, Hou Hanshu) recorded that in 166 AD the first envoy from the Roman Empire to China arrived by this route, and merchants were soon to follow. The 3rd-century "Tales of Wei" (Ngụy Lục, Weilue) mentioned a "water route" (the Red River) from Jiaozhi into what is now southern Yunnan. From there, goods were taken overland to the rest of China via the regions of modern Kunming and Chengdu.
At the same time, in present-day central Vietnam, there was a successful revolt of Cham nations. Chinese dynasties called it Lin-Yi (Lin village). It later became a powerful kingdom, Champa, stretching from Quảng Bình to Phan Thiết (Bình Thuận).
In the period between the beginning of the Chinese Age of Fragmentation to the end of the Tang Dynasty, several revolts against Chinese rule took place, such as those of Lý Bôn and his general and heir Triệu Quang Phục; and those of Mai Thúc Loan and Phùng Hưng. All of them ultimately failed, yet most notable were Lý Bôn and Triệu Quang Phục, whose Anterior Lý Dynasty ruled for almost half a century (544 AD to 602 AD) before the Chinese Sui Dynasty reconquered their kingdom Vạn Xuân.
Early in the 10th century, as China became politically fragmented, successive lords from the Khúc family, followed by Dương Đình Nghệ, ruled Giao Châu autonomously under the Tang title of Tiết Độ Sứ, Virtuous Lord, but stopping short of proclaiming themselves kings.
In 938, the kingdom of Southern Han sent troops to conquer autonomous Giao Châu. Ngô Quyền, Dương Đình Nghệ's son-in-law, defeated the Southern Han fleet at the Battle of Bach Dang River (938). He then proclaimed himself King Ngô and effectively began the age of independence for Vietnam.
Ngô Quyền's untimely death after a short reign resulted in a power struggle for the throne, the country's first major civil war, The upheavals of Twelve warlords (Loạn Thập Nhị Sứ Quân). The war lasted from 945 AD to 967 AD when the clan led by Đinh Bộ Lĩnh defeated the other warlords, unifying the country. Dinh founded the Đinh Dynasty and proclaimed himself First Emperor (Tiên Hoàng) of Đại Cồ Việt (Hán tự: 大瞿越; literally "Great Viet Land"), with its capital in Hoa Lư (modern day Ninh Bình). However, the Chinese Song Dynasty only officially recognized him as Prince of Jiaozhi (Giao Chỉ Quận Vương). Emperor Đinh introduced strict penal codes to prevent chaos from happening again. He tried to form alliances by granting the title of Queen to five women from the five most influential families.
In 979 AD, Emperor Đinh Bộ Lĩnh and his crown prince Đinh Liễn were assassinated, leaving his lone surviving son, the 6-year-old Đinh Toàn, to assume the throne. Taking advantage of the situation, the Chinese Song Dynasty invaded Đại Cồ Việt. Facing such a grave threat to national independence, the court's Commander of the Ten Armies (Thập Đạo Tướng Quân) Lê Hoàn took the throne , founding the Former Lê Dynasty. A capable military tactician, Lê Hoan realized the risks of engaging the mighty Chinese troops head on; thus he tricked the invading army into Chi Lăng Pass, then ambushed and killed their commander, quickly ending the threat to his young nation in 981 AD. The Song Dynasty withdrew their troops yet would not recognize Lê Hoàn as Prince of Jiaozhi until 12 years later; nevertheless, he is referred to in his realm as Đại Hành Emperor (Đại Hành Hoàng Đế). Emperor Lê Hoàn was also the first Vietnamese monarch who began the southward expansion process against the kingdom of Champa.
Emperor Lê Hoàn's death in 1005 AD resulted in infighting for the throne amongst his sons. The eventual winner, Lê Long Đĩnh, became the most notorious tyrant in Vietnamese history. He devised sadistic punishments of prisoners for his own entertainment and indulged in deviant sexual activities. Toward the end of his short life – he died at 24 – Lê Long Đĩnh became so ill that he had to lie down when meeting with his officials in court.
When the king Lê Long Đĩnh died in 1009 AD, a Palace Guard Commander named Lý Công Uẩn was nominated by the court to take over the throne, and founded the Lý dynasty. This event is regarded as the beginning of a golden era in Vietnamese history, with great following dynasties. The way Lý Công Uẩn ascended to the throne was rather uncommon in Vietnamese history. As a high-ranking military commander residing in the capital, he had all opportunities to seize power during the tumultuous years after Emperor Lê Hoàn's death, yet preferring not to do so out of his sense of duty. He was in a way being "elected" by the court after some debate before a consensus was reached.
Lý Công Uẩn, posthumously referred as Lý Thái Tổ, changed the country's name to Đại Việt (Hán tự: 大越; literally "Great Viet"). The Lý Dynasty is credited for laying down a concrete foundation, with strategic vision, for the nation of Vietnam. Leaving Hoa Lư, a natural fortification surrounded by mountains and rivers, Lý Công Uẩn moved his court to the new capital in present-day Hanoi and called it Thăng Long (Ascending Dragon). Lý Công Uẩn thus departed from the militarily defensive mentality of his predecessors and envisioned a strong economy as the key to national survival. Successive Lý kings continued to accomplish far-reaching feats: building a dike system to protect the rice producing area; founding Quốc Tử Giám, the first noble university; holding regular examinations to select capable commoners for government positions once every three years; organizing a new system of taxation; establishing humane treatment of prisoners. Women were holding important roles in Lý society as the court ladies were in charge of tax collection. The Lý Dynasty also promoted Buddhism, yet maintained a pluralistic attitude toward the three main philosophical systems of the time: Buddhism, Confucianism, and Taoism. During the Lý Dynasty, the Chinese Song Dynasty officially recognized the Đại Việt monarch as King of Giao Chỉ (Giao Chỉ Quận Vương).
The Lý Dynasty had two major wars with Song China, and a few conquests against neighboring Champa in the south. The most notable battle took place on Chinese territory in 1075 AD. Upon learning that a Song invasion was imminent, the Lý army and navy totalling about 100,000 men under the command of Lý Thường Kiệt, Tông Đản used amphibious operations to preemptively destroy three Song military installations at Yong Zhou, Qin Zhou, and Lian Zhou in present-day Guangdong and Guangxi, and killed 100,000 Chinese. The Song Dynasty took revenge and invaded Dai Viet in 1076, but the Song troops were held back at the Battle of Như Nguyệt River commonly known as the Cầu river, now in Bắc Ninh province about 40 km from the current capital, Hanoi. Neither side were able to force a victory, so the Lý Dynasty proposed a truce, which the Song Dynasty accepted.
Toward the end of the Lý Dynasty, a powerful court minister named Trần Thủ Độ forced king Lý Huệ Tông to become a Buddhist monk and Lý Chiêu Hoàng, Huệ Tông's young daughter, to become queen. Trần Thủ Độ then arranged the marriage of Chiêu Hoàng to his nephew Trần Cảnh and eventually had the throne transferred to Trần Cảnh, thus begun the Trần Dynasty. Trần Thủ Độ viciously purged members of the Lý nobility; some Lý princes escaped to Korea, including Lý Long Tường.
After the purge most Trần kings ruled the country in similar manner to the Lý kings. Noted Trần Dynasty accomplishments include the creation of a system of population records based at the village level, the compilation of a formal 30-volume history of Đại Việt (Đại Việt Sử Ký) by Lê Văn Hưu, and the rising in status of the Nôm script, a system of writing for Vietnamese language. The Trần Dynasty also adopted a unique way to train new kings: as a king aged, he would relinquish the throne to his crown prince, yet holding a title of August Higher Emperor (Thái Thượng Hoàng), acting as a mentor to the new Emperor.
During the Trần Dynasty, Đại Việt repelled three invasions in 1257 AD, 1284 AD, and 1288 AD by the Mongols under Kublai Khan, who had occupied China and founded the Yuan dynasty. The key to Đại Việt's successes was to avoid the Mongols' strength in open field battles and city sieges - the Trần court abandoned the capital and the cities. The Mongols were then countered decisively at their weak points, which were battles in swampy areas such as Chương Dương, Hàm Tử, Vạn Kiếp and on rivers such as Vân Đồn and Bạch Đằng. The Mongols also suffered from tropical diseases and loss of supplies to Trần army's raids. The Yuan-Trần war reached its climax when the retreating Yuan fleet was decimated at the Battle of Bach Dang (1288). The military architect behind Dai Viet's victories was Commander Trần Quốc Tuấn, more popularly known as Trần Hưng Đạo.