A "remarkable medieval chapel" is to undergo structural repairs to ensure its survival, Historic England said.
Grade I listed Becket's Chapel, in Wymondham, Norfolk, had in its time been a church, a school, a prisoner lock-up and a library.
The current building, which dates from 1300 to 1350, is in a state of decay and needs urgent structural work.
Trudi Hughes, of Historic England, said: "This striking building has such a story to tell."
A grant of £400,000 from the conservation body will allow the building to be bought and restored by the Norfolk Historic Buildings Trust (NHBT) before it starts a new life as a community hub.
Ms Hughes, Historic England Heritage at Risk surveyor, said: "We've found a way to save and repair this remarkable medieval chapel using traditional repairs and techniques combined with measures to reduce dampness and improve its carbon footprint.
"This striking building has such a story to tell, having had a fascinating and varied history.
"It's wonderful that this gem of a building will once again be at the heart of its local community."
The building was added to Historic England's Heritage at Risk register in 2018, with urgent repairs needed to the roof, gutters, drainage and masonry.
Dedicated to St Thomas à Becket, the chapel is thought to have been founded in the late 12th Century by the son of William d'Aubigny, the founder of Wymondham Abbey.
It was rebuilt in 1400 and converted to a school in 1559.
The chapel became a gaol from the 17th Century until 1848. In 1873, the building was restored and used as a public hall and school.
More recently, it was used as the Wymondham branch library and then the town's arts centre.
Judith Harwood, of the NHBT, said the chapel was "of significant architectural and historical importance".
"This is a ground-breaking project which supports the drive to net zero by incorporating insulation, locally sourced materials where possible and, we hope, in the second phase, powered by renewable energy."
Gavin Richards, of the Architectural Heritage Fund, which has supported the project since 2018, said the restoration will "mean the chapel will be a wonderful asset for local residents and visitors all year round."