Elia Viviani (Deceuninck-Quick Step) was second and Rudiger Selig (BORA-hansgrohe) took third place ahead of Australian Caleb Ewan (Lotto Soudal).
“I knew it would be a fast sprint with the last 20km very nervous," Demare said. "To avoid any crash, I asked my team-mates to keep me in the first ten or fifteen positions.
"A sprint like this, at full speed with no need to brake, was perfect for me. I and my team would have liked to win a sprint earlier in the Giro. I was always placed but I wasn’t winning. It’s a relief today.
"My goal is to reach Verona. If I have the legs to overcome the mountains, I’ll be there till the end.”
Points leader Pascal Ackermann (BORA-hansgrohe) crashed just inside the final kilometre and could not contest the sprint.
Valerio Conti (UAE Team Emirates) retained the overall leader’s pink jersey ahead of Primoz Roglic (Jumbo-Visma) and Nans Peters (AG2R) on a stage designed to get the legs restarted after the first rest day.
“In some stages of this Giro we rode very hard but it wasn’t the case today," Conti said. "I don’t like this kind of racing because it makes the finale more dangerous as many riders are still fresh and too many believe they can win.
"I don’t want to take unnecessary risks. I repeat that I want to keep the Maglia Rosa as long as possible."