The town was originally an settlement of Old Prussians known as "Lecbarg" until being conquered in 1240 by the Teutonic Knights, who named it Heilsberg. In 1306 it became the seat for the Bishopric of Warmia, and remained the Prince-Bishop's seat for 500 years. In 1309 the settlement received town privileges. In the 1350s the Castle of Warmian Bishops was built, and it was expanded in the following centuries, becoming one of the most significant and remarkable historic monuments of Warmia, which nowadays houses a museum and is listed as a Historic Monument of Poland. In 1440 the town joined the anti-Teutonic Prussian Confederation, upon the request of which in 1454 Polish King Casimir IV Jagiellon incorporated the region and town to the Kingdom of Poland. This caused the Polish–Teutonic Thirteen Years' War, as a result of which in the Second Peace of Thorn (1466) the Teutonic Order ended its claim to the area and recognized it as part of Poland. It was the capital of the Prince-Bishopric of Warmia in the Polish province of Royal Prussia in the larger Greater Poland Province of the Polish Crown.