Italian Elia Viviani was second and Rudiger Selig of Germany took third place ahead of Australian Caleb Ewan.
"We came to the Giro for this. I really wanted a stage win and I got it," said Demare.
"I avoided the crash because we, as a team, were very well positioned. I'm super happy."
German Pascal Ackermann crashed just inside the final kilometre and could not contest the sprint.
Italian Valerio Conti retained the overall leader's pink jersey.
"I have been well protected by my team the whole day. I thank them for giving me one more day in the Maglia Rosa," said Conti.
(Reporting by Julien Pretot; editing by Ed Osmond and Ken Ferris)
Share
