It was first mentioned in an 1146 chronicle as a part of Principality of Chernigov. Kozelsk became famous in the spring of 1238, when its seven-year-old prince Vasily, son of Titus, had to defend the town against the army of Batu Khan. The latter dubbed it an "evil town" because its citizens had been fighting the attackers for seven weeks in a row, killing around four thousand enemy soldiers during the siege. The citizens of Kozelsk were greatly outnumbered and almost all of them died in battle.
In 1446, Kozelsk was temporarily under the rule of the Grand Duchy of Lithuania. In 1494, the town was finally annexed by the Grand Duchy of Moscow. In 1607, one of Ivan Bolotnikov's units was located in Kozelsk and showed resistance to the Tsar's army.
The much-venerated monastery, Optina Pustyn, is close by. In the 19th century, this hermitage gained wide renown for its "startsy". After the outbreak of World War II, a POW camp was established in the monastery for Polish officers taken captive by the Red Army during the Polish Defensive War of 1939. Between April and May 1940, the NKVD transferred approximately 5,000 of them to a forest near Katyn, where they were executed in what became known as the Katyn massacre. The remaining two hundred officers were sent to a camp in Pavlishchev Bor and then to Gryazovets. The town was occupied by the German army from October 8, 1941 until December 27, 1941 and was totally destroyed. It was rebuilt after the war.
Within the framework of administrative divisions, Kozelsk serves as the administrative center of Kozelsky District, to which it is directly subordinated. As a municipal division, the town of Kozelsk is incorporated within Kozelsky Municipal District as Kozelsk Urban Settlement.
After World War II, Kozelsky District became the home for the 28th Guards Rocket Division of the Strategic Missile Troops. Up to a third of the population of Kozelsk was connected in one way or another with the missile division.