Frenchman Alaphilippe left his sprinting line in a group of five - hampering Swiss Marc Hirschi in the process - and with victory in sight he stopped pedalling, making a beginner's mistake.
Slovenian Roglič , who lost the Tour's yellow jersey in the final time trial earlier this month, pursued his effort all the way to the line and crossed first.
Alaphilippe was later demoted to fifth place - the last spot in the leading group - for moving out of his line and impeding Hirschi.
That meant Hirschi, the world championship's road race silver medallist and recent winner of the Fleche Wallonne classic, was second with Tour champion Tadej Pogačar of Slovenia taking third place.
"It's unbelievable, it was so close. Never stop believing. It's a few centimetres, it's incredible that I managed to win," said Roglic.
"In the end I managed to win something, no?"
While Hirschi was diplomatic in the post-race interview, saying 'for sure (Alaphilippe) took my wheel, he moved a little bit but I haven't seen it on TV,' Pogačar was less than ecstatic about his promotion to third place, inferring he could've won if he was not impeded by the Frenchman's swashbuckling swerves.
"Mixed feelings after that finish," he said. "I felt good all day. Then in the finish I felt good and we start sprinting and I saw myself in a good posiiton.
"I go for the win, and one moment just changed everything, 'I'm going to crash, I'm going to crash,' but then they disqualified Alaphilippe and I got third place and a bit of mixed feelings right now."
For his first race in the world champion's rainbow jersey, Alaphilippe certainly expected a different outcome. The Frenchman's big attack had been widely expected, and eventually came 500 metres from the top of the Côte de la Roche aux Faucons, the last climb, 13 kilometres from the finish line.
Hirschi managed to follow, with Pogacar and Roglic joining them further down the road.
Another Slovenian, Matej Mohoric, then joined them in the final straight and launched the sprint that Alaphilippe initially thought he had won.