Loket, originally called Stein-Elbogen due to its rocky location, is said to have been founded in 870 by the margraves of Vohenburg who were then related to the dukes of Bavaria to whom the entire Elbogen districts belonged until the 12th century.
The first written mention of Loket as a town comes from a 1234 deed when the first known royal Loket burgrave was recorded. According to archeological investigations, the foundation of the stone castle dates back to the third quarter of the 12th century, during the reign of Přemysl Otakar I, either by the Czech Prince Vladistav I, later the Czech King Vladislaus II of Bohemia, or by ministerial officials to the Emperor Fridrich I Barbarosa. The old romanesque castle comprised two towers, a church and a building standing on the site of the present Margrave's House. The church stood beneath the present castle where St. Wenceslaus church is standing today. The other tower, no longer existent, stood to the north-east of the castle. Above all, the castle served as protection to the merchant's path leading from Prague through Cheb and on to Plauen and Erfurt, but after the re-annexation by the Czech state it began functioning as a frontier fortress. By this time it became the new administrative centre of the region.
By the turn of the 13th century a settlement was built around the castle walls and later raised into a royal town. From the 1250s the castle was gradually enlarged and the formerly Romanesque building turned into a Gothic stronghold which was often visited by the members of the royal family.