The history of Wilton dates back to the Anglo-Saxons in the 8th century AD, and by the late 9th century it was the capital of "Wiltunscire", a shire of the Kingdom of Wessex. It remained the administrative centre of Wiltshire until the 11th century. Wilton was of significant importance to the church, with the founding of Wilton Abbey in AD 771 amongst other establishments. In AD 871 Alfred the Great fought and lost an important battle here against the Danish armies, leaving him in retreat for several years.