Marini's time of 1:44.782s in afternoon practice denied title protagonist Jorge Martin the number one spot by only eight-thousandths of a second at the Buddh International Circuit on Friday.
Marquez's factory Honda team-mate, Joan Mir, secured automatic progress to Qualifying 2 for the first time this season, with the 2020 world champion finishing 10th on the time sheets.
Championship leader Francesco Bagnaia was seventh on the Lenovo Ducati ahead of Yamaha's Fabio Quartararo.
Marco Bezzecchi, who led FP1, was fifth on the second of the satellite VR46 Ducati machines.
Like FP1, second practice was held over an extended 70-minute session.
Australia's Jack Miller was an early faller at Turn 12 on the Red Bull KTM and was outside the Q2 spots at the conclusion of Friday, ending the day in 18th.
Bezzecchi picked up where he left off and topped the times after 10 minutes from Pramac Ducati's Martin. In a recurring theme from FP1, Bezzecchi was caught out at Turn 1 and ran into the gravel without crashing.
Espargaro then went to the summit on the Aprilia in 1:46.182 before the Spaniard overshot at Turn 1, taking an excursion into the gravel.
Martin picked up his pace and hauled himself to the top with a lap in 1:46.182 after following Espargaro around.
Espargaro then recorded the fastest lap on the factory RS-GP23 before the pendulum swung again, with Bezzecchi eclipsing the Spaniard's time before taking another unplanned trip off the track at the first corner.
With just over half-an-hour to go, Martin improved with a 1:45.672 to lead Espargaro by 0.255s, while Bezzecchi was third, three-tenths down on Martin.
Bagnaia made strong progress as he climbed to fifth, almost six-tenths off Martin.
Marini was also on the charge and set his best lap to go second behind Martin, but after crossing the line he overshot at Turn 1 and lost the front of his Ducati, tumbling off at low speed.
As the time attacks began, Maverick Viñales (Aprilia Racing) did a 1:45.361 and then, almost predictably, ran off the track into the gravel at Turn 1 as he held sway by three-tenths from Martin.
With all the riders using the soft rear tyre, Bezzecchi raised the bar with a 1:45.246s, 0.115s quicker than Viñales, while Fabio Di Giannantonio was third on the Gresini Ducati with 13 minutes left on the clock.
Quartararo, who was plagued by reported gearbox issues in FP1 and only completed 10 laps, moved into second with 10 minutes remaining on the Monster Energy Yamaha, only 0.045s down on Bezzecchi.
Bagnaia was languishing in 18th going into the final runs with seven minutes left.
Martin blasted to the fastest time with the first 1m 44s lap of the weekend, clocking 1:44.790s to lead Marquez. Like several riders before him, Martin struggled to get stopped for Turn 1 after crossing the line.
Espargaro went above Marquez into second with Bezzecchi pushed to fourth as the minutes ticked down.
Viñales was in fifth ahead of the ever-improving Bagnaia.
With seconds remaining, Marini pulled a big lap out of the bag in 1:44.782s to claim the top spot from Martin.
Japan's Takaaki Nakagami was a faller at Turn 9 on the LCR Honda resulting in a yellow flag, which prevented Marini's lap being bettered in the dying seconds.
South African Brad Binder's time would have put him fourth on the Red Bull KTM but his lap was cancelled due to the yellow flag.
Viñales finished sixth ahead of Bagnaia and Quartararo, while Johann Zarco (Pramac Ducati) and Mir rounded out the top 10. Di Giannantonio just missed out on Q2 in 11th.