The Frenchman, a 13-time Dakar champion in all categories, won the 266-kilometer (165-mile) stage between San Juan de Marcona and Arequipa in Peru in 2 hours, 51 minutes, 19 seconds.
He got going after nearly half an hour at a standstill, but his Peugeot was soon stuck again and lost another two hours waiting for assistance.
"The sand was extremely loose", Loeb said. "We tried to put air back in and we were stuck again for 30 or 20 minutes". We got to a crest where another vehicle was stuck.
"The vehicle was going really well". And, bam, we went right into the hole. "We are doing a good job at the moment though". And at the first taste of the high-speed stages in the Dakar, the man has pulled out all stops to register his first win in Dakar 2018. Sainz, also in a Peugeot and a double world rally champion, got bogged down in the dunes early on and was 18 minutes behind Peterhansel at the finish.
Toyota endured a hellish fourth stage with two-time Dakar victor Nasser Al-Attiyah and team-mate Giniel de Villiers both stopping on the course for separate issues and lost a huge chunk of time. Stephane Peterhansel of France and copilot Jean Paul Cottret of France drive their Peugeot.
De Villiers posted the third-fastest time on the stage, about five minutes ahead of Sainz, despite suffering a puncture mid-stage.
The bigger threat to Peterhansel's supremacy on the day had come from Yazeed Al-Rajhi of X-raid Mini. "We had two flat tires at the beginning and afterwards we took care because we didn't have any spares", al-Attiyah told the Dakar website.
No major oil spill after tanker collision: China
The tanker Sanchi was carrying nearly 1 million barrels of condensate, an ultra-light, highly flammable crude oil, to South Korea. The 21 Chinese crew members of the CF Crystal vessel, which had been carrying grain from the United States, were rescued.
"The group I was in missed a waypoint so we lost a fair amount of time getting back on track".
Al-Attiyah quickly found his rhythm again, however, and clawed back time throughout the remainder of the stage to finish fifth, having lost only 16 minutes to Peterhansel despite the gearbox change.
Qatar's Nasser al-Attiyah, a two times Dakar victor and leader after the opening day, fell to fourth in his Toyota and was 58 minutes adrift of 13 times victor Peterhansel, who was third in the stage.
Joan Barreda Bort moved up from 14th fourth overall with a masterful ride on Stage 5. He finished the stage at 33rd position, which placed him at 24 position in the overall rankings.
"It was a very hard day".
Stephane Peterhansel extended his overall lead of the Dakar Rally after winning the fifth stage on Wednesday. The KTM rider has been transported by medical helicopter to the bivouac after being ruled out of finishing the fourth stage.
Toby Price during Stage 4 of the 2018 Dakar Rally.