Mielnik has a long and rich history. The area of the town was in c. 1018 captured by warriors of Bolesław Chrobry, and remained in Polish hands until c. 1050, when Kazimierz Odnowiciel handed it to Grand Prince of Kiev, Yaroslav the Wise, for his help in getting rid of Miecław. Since Mielnik was located near the settlement of the Yotvingians, its area was subject to frequent raids.
On 1 October 1501, Alexander I Jagiellon named first vogt of Mielnik, a nobleman named Mikołaj Rychlik, who resided in a manor house at Osłowo. On October 23 of the same year, the Union of Mielnik was signed by Alexander I and Lithuanian nobles. Four days later, Alexander changed Mielnik's town charter into Magdeburg rights. King Sigismund I the Old visited Mielnik twice, in 1506 (while awaiting Polish crown), and 1513, when he stayed here for a month, ordering construction of a second Catholic church.
In the 16th century, Mielnik established its position as one of the most important towns of the province of Podlasie. In 1520 it became seat of a powiat, and in 1545, three Russian boyars, Fiedor Owczyna, Jendriej Palecki and Michalko Obolenski were imprisoned in the Mielnik Castle. The castle itself was expanded and remodeled in the 1540s and early 1550s by starosta Nikodem Janowicz Swiejko of Ciechanowiec. Since 1551, it was a Renaissance complex, used for royal visits. In 1554, a bridge over the Bug river was destroyed, and a new one was built in Turna. As a result, merchants with their goods bypassed Mielnik, which contributed to the town's slow decline. In 1566, the Mielnik Land, a territorial unit within the Podlaskie Voivodeship, was established.
Following the act of restoration of the Podlasie region to the Kingdom of Poland and Union of Lublin (1569), Mielnik passed to the Kingdom of Poland. Its population was c. 1,500, and the town remained an important urban center of Podlasie.
Swedish invasion of Poland was a disaster for Mielnik, and the town has never recovered from the destruction. In late May 1657, it was burned to the ground by Swedes of Gustav Otto Steinbock and Transilvanians of George II Rakoczi, who also destroyed the royal castle and the parish church. As a result, Mielnik lost 70% of population.
Following the Partitions of Poland, Mielnik was annexed by the Kingdom of Prussia (1795), and in 1807, it was transferred to the Russian Empire, where it remained until World War I. In 1829, Russians destroyed the local Catholic church, and in 1863, to commemorate the suppression of the January Uprising, the Orthodox chapel of Alexander Nevsky was built. Mielnik returned to Poland in 1919, losing its town charter in 1934.