Hamelin started with a monastery, which was founded as early as 851 AD. A village grew in the neighbourhood and had become a town by the 12th century. The incident with the "Pied Piper" (see below) is said to have happened in 1284 and may be based on a true event, although somewhat different from the tale. In the 15th and 16th centuries Hamelin was a minor member of the Hanseatic League.

Hamelin was surrounded by four fortresses, which gave it the nickname "Gibraltar of the North". It was the most heavily fortified town in the Electorate of Hanover. The first fort (Fort George) was built between 1760 and 1763, the second (Fort Wilhelm) in 1774, a third in 1784, and the last (called Fort Luise) was built in 1806.

In 1808, Hamelin surrendered without fighting to Napoleon, after his victory at the Battle of Jena-Auerstedt. Napoleon's forces subsequently pulled down the town's historic walls and guard towers. In 1843, the people of Hamelin built a sightseeing tower on the Klüt Hill, out of the ruins of Fort George. This tower is called the Klütturm and is a popular sight for tourists.

The town is famous for the folk tale of the Pied Piper of Hamelin (German: Der Rattenfänger von Hameln), a medieval story that tells of a tragedy that befell the town in the 13th century. The version written by the Brothers Grimm made it popular throughout the world; it is also the subject of well-known poems by Goethe and Robert Browning. In the summer every Sunday, the tale is performed by actors in the town centre.