A short, sharp shower right before the start of the session forced riders to start on wet tyres.
The field soon started to switch to slicks as the track began to dry, only for another period of torrential rain to arrive and soak the track again.
Grand Prix Germany
Eighth win in a row for Marquez at Sachsenring
Marquez was one of a handful of drivers to sneak a lap in on drys before the rain returned and set a 1m29.472s to snatch top spot from Honda team-mate Pedrosa, who had previously led the early stages.
He remained at the top for the majority of the remainder of the session, before the track dried sufficiently to allow riders to make improvements in the last 10 minutes.
Pramac Ducati rider Danilo Petrucci was first to shoot to the top of the times, before Aprilia's Aleix Espargaro and then points leader Andrea Dovizioso demoted him.
Marquez rocketed back to the top of the pile with a 1m28.451s, a time on which he twice improved, but Barbera eventually outpaced him to end the session fastest by 0.063 seconds with a best time of 1m28.115s.
Hector Barbera, Ducati Aspar, MotoGP 2017-2018
Image credit: Getty Images
Marquez settled for second ahead of Pedrosa, with he and fourth-place man Petrucci recording identical times of 1m28.468s.
Aleix Espargaro was fifth ahead of LCR Honda's Cal Crutchlow, with Ducati rider Dovizioso best placed of the works Ducatis.
Jonas Folger was eighth, with his Tech 3 Yamaha team-mate Johann Zarco the only rider to crash in the session as he fell during the second rain shower.
Sam Lowes brought a second Aprilia into the top 10 in ninth, ahead of the second works Ducati Jorge Lorenzo.
The factory Yamaha riders had a low-key session, with Valentino Rossi and Maverick Vinales only 15th and 20th.
Grand Prix Germany
Marquez extends unbeaten Germany pole run
Rossi announces decision to retire at end of season