The town was established in 1428 as a German farming village "Auf der Gaylen", in the monastic state of the Teutonic Knights. In 1480 a church was built there. In the 16th century it grew rapidly in the Duchy of Prussia, as trading increased between the Germans and Poles. In the middle of the century there were 38 farms and three mills. In 1645 it gained the right to have four markets per year. In 1656 it was pillaged by the Tatars, plundered and burned, and many residents died or were displaced. It became part of the Kingdom of Prussia in 1701.
From 1709-1711, there was a plague epidemic, which killed 315 residents. Nonetheless, the economic development continued apace, and the settlement received its town charter in 1722 from King Frederick William I of Prussia. A royal resettlement program brought many new residents, especially artisans. Between 1756 and 1763, a new church, a 1.65 ha marketplace, a network of roads, and a new town hall were built. During the Seven Years' War, the town was occupied by Russian troops from 1758-1762. Afterwards, it became a garrison city for Prussian troops from 1764 to 1800.
In the Napoleonic Wars, Russia made the town a headquarters for their troops in 1807 for nine days, followed by French and Polish troops, demanding payment of their stationing costs, as well as bringing new sicknesses, which killed many residents. After the defeat of the French, the Prussians reorganized their territorial administration, and in 1818 the town was assigned to Landkreis Johannisburg in the Gumbinnen region. It became part of the German Empire during the unification of Germany. In 1885 the railway line from Johannisburg to Lyck arrived in the town. At that time, the population was about 1,700, of whom 700 were Polish-speaking.
During World War I, combat between German and Russian troops happened near the town, which was penetrated by Russian troops and plundered. Under the terms of the Treaty of Versailles, the residents of the town voted 100% to remain in Weimar Germany. In 1927 and 1928, a new couthouse and waterworks were built. In the Germanization programs of the Nazi era, the town reverted to the "German" name Gehlenburg. In the 1939 census, there were 2,823 residents. It was nearly abandoned in the face of the advancing Soviet front, which captured the city on January 23, 1945. A few weeks later, power was transferred to the new Polish administration. The town was annexed according to the Potsdam Conference and given its modern name. The remaining native German populace was expelled and replaced with Poles.