Lotto-Soudal rider Greipel overhauled Cavendish and Norwegian Alexander Kristoff and held off fast-finishing Slovak Peter Sagan, who came second.
"He was faster," said Cavendish of Greipel. "Today I was just beaten."
It was Greipel's second stage victory this year, while Tony Martin remains overall leader from Britain's 2013 champion Chris Froome.
Team Sky's Froome finished safely in the peloton at the end of the stage from Arras to Amiens to remain second in the general classification, 12 seconds behind Martin.
The distance between Froome and his main GC rivals - Alberto Contador, Vincenzo Nibali and Nairo Quintana - remained unchanged.
Greipel's victory continued a successful few days for German riders after Etixx - Quick-Step's Martin claimed yellow on Tuesday when he made a late surge to win stage four in Cambrai.
"Three stages and the yellow jersey for Germany, it's unbelievable," said Martin. "I would wish Cav could beat Andre today, but if Cav can't win, I'm so happy that a German rider's in front."