Prehistory and origin of Stockholm
According to the Chronicle of Eric, written in the 1320s, Stockholm was founded by Birger Jarl around 1250 as a lock to the Lake Mälaren region ("laas fore then sio", "lock before that lake") in order to prevent pirates from reaching the seven cities and nineteen parishes around it. Another medieval source ("Visbyannalerna"), however, claims the city was founded in 1187 following a pagan pillaged the city of Sigtuna, and as there was an "Earl" named Birger around at that time too, the disputed origin of the city are likely to remain obscure and some historians choose a diplomatic interpretation saying there was some sort of fortification around by the mouth of lake Mälaren when the city was founded during the second half of the 13th century. As Snorri mentions no city in his account but some sort of fortification called a "kastali" (in various manuscripts curiously said to be located east and west of "Stocksund"), it has generally been assumed this fortification eventually developed into the castle Tre Kronor located where still is the Stockholm Palace.
Gamla stan
Stockholm derives its mythological origin from a dwelling place called "Agnefit". As the second element "fit" means 'moist meadow', this place was supposedly located on the western shore of today's Stadsholmen (arguably the only possible location for a meadow at the time). The first element of this name is, explains the historian Snorri Sturluson (1178–1241), derived from King Agne, a presumably mythological king who, in a dim and distant past (around 400 A.D. according to some historians), encamped here after having successfully raided Finland. His intentions were to marry "Skjalf", the daughter of the defeated Finnish chieftain. The young woman, however, tricked him to arrange a celebration including prominent guests which eventually turned into a boozing party, and, while Agne slept in a drunken stupor, Skjalf had him hanged in his gold necklace before escaping.
While the reliability of this story remains disputed, dendrochronological examinations of logs found on Helgeandsholmen just north of Stadsholmen in 1978-1980, concluded these trees were cut down during the period 970-1020, most of them from the later part of that period, and these logs presumably gave the entire city its present name, "Stock-holm", "Log-Islet".