The American rock outfit will play the Pyramid stage on Saturday, 24 June, confirming rumours they would take the slot.
The announcement was made by the group during a secret show for fans in Frome, Somerset, on Friday night - their first in 18 months.
They join the line-up alongside Radiohead.
The announcement was also viewed by fans on a live internet stream of the Frome gig, with a video that saw the band sitting on the festival site four months early.
Festival organiser Michael Eavis introduced them on stage, confirming they were playing.
Singer and guitarist Dave Grohl said: "So, who knew? How did you know? You just can't keep a... secret any more."
BBC Radio DJ Jo Whiley tweeted a picture from the gig and wrote: "A very blurry Michael Eavis announcing @foofighters will be headlining @GlastoFest Saturday night. A surprise, I know."
A previous version of this story mistakenly said Ed Sheeran and Green Day have also been confirmed. This is an error and was not based on any official sources.
Foo Fighters will be headlining Glastonbury Festival 2017, the band have announced.