The 37-year-old, who also took stage 12, was followed home by second-placed Sam Bennett (Bora-Hansgrohe) and teammate Remi Cavagna (Deceuninck-Quick-Step).
“It was a very special one. I think it’s the kind of stage that will stay in the history, because of the way we rode. It was crazy from the gun,” said Gilbert.
“We went with a big group of more than 40 guys, with some guys there for GC, with Nairo Quintana and James Knox for us. We were like seven out of eight guys from Deceuninck-Quick-Step and, honestly, we rode like crazy as a team, giving morale to each other – it was really nice to see.
“With the passing kilometres we dropped guys, on different difficulties – crosswinds or climbs.”
Zdenek Śtybar (Deceuninck-Quick-Step) attacked with 2km to the line, but the Czech rider was caught inside the final 500m.
Bennett then opened up his sprint early, with Gilbert on his wheel.
The stage winner came off his wheel over the final 200m and surged ahead to clinch the victory.
All four riders were part of the large breakaway group of the day, which formed soon after the start of the 219.6km stage and had a maximum gap of nearly six minutes.
Primoz Roglic (Jumbo-Visma) retained the overall lead and will head into his eighth day in red.
Quintana jumped up four spots in the overall standings, while Alejandro Valverde (Movistar Team) moved down one.
View more results below.
Stage 17 results
1. Philippe Gilbert (Bel) Deceuninck-Quick-Step 4:20:15
2. Sam Bennett (Irl) Bora-Hansgrohe +0:00:02
3. Rémi Cavagna (Fra) Deceuninck-Quick-Step
4. Dylan Teuns (Bel) Bahrain-Merida
5. Wilco Kelderman (Ned) Team Sunweb
6. Jonas Koch (Ger) CCC Team
7. Lawson Craddock (USA) EF Education First
8. Tim Declercq (Bel) Deceuninck-Quick-Step
9. Silvan Dillier (Swi) AG2R La Mondiale
10. James Knox (GBr) Deceuninck-Quick-Step +0:00:06
90. Nicholas Dlamini (RSA) Team Dimension Data +0:23:36
123. Willie Smit (RSA) Katusha-Alpecin +0:29:21
128. Jaco Venter (RSA) Team Dimension Data
135. Louis Meintjes (RSA) Team Dimension Data
1. Primoz Roglic (Slo) Team Jumbo-Visma 66:43:36
2. Nairo Quintana (Col) Movistar Team +0:02:24
3. Alejandro Valverde (Spa) Movistar Team +0:02:48
4. Tadej Pogacar (Slo) UAE Team Emirates +0:03:42
5. Miguel Angel Lopez (Col) Astana Pro Team +0:03:59
6. Wilco Kelderman (Ned) Team Sunweb +0:05:05
7. Rafal Majka (Pol) Bora-Hansgrohe +0:07:40
8. James Knox (GBr) Deceuninck-Quick-Step +0:08:03
9. Carl Fredrik Hagen (Nor) Lotto Soudal +0:10:43
10. Dylan Teuns (Bel) Bahrain-Merida +0:12:21
45. Louis Meintjes (RSA) Team Dimension Data +1:42:34
100. Nicholas Dlamini (RSA) Team Dimension Data +2:50:10
110. Willie Smit (RSA) Katusha-Alpecin +3:08:05
136. Jaco Venter (RSA) Team Dimension Data +3:31:52