Safina triumphed 6-7 (6/8), 7-6 (7/5), 6-2, saving a match point along the way, in a tie which lasted almost three hours and will now meet fellow Russian Elena Dementieva, the seventh seed, for a place in the semi-finals.
The victory echoed Safina's three-set win over Sharapova at the same stage in 2006 when she recovered from 1-5 down in the final set to triumph.
Sharapova, who still needs a French Open title to complete a career Grand Slam, went into Monday's tie still searching for top gear on the testing Paris clay on which her best performance came last year when she made the semi-final.
The 21-year-old needed three sets to get past her first and second round opponents while Safina, who came into Paris on a high after winning the Berlin claycourt event, had won all three previous rounds in straight sets.
Sharapova broke to lead 4-3 in the first set when a Safina double fault, topped off by a crunching crosscourt backhand, put the top seed in charge.
However, Safina carved out five break points in the eighth game to level the tie on a damp, humid Suzanne Lenglen Court.
Safina then squandered two set points in the tie-break when she led 6/4, and Sharapova made her pay by claiming the next four points to take the opener when her opponent went wide with a lob.
Safina carved out a break in the first game of the second set before heavy rain forced the players off court for 90 minutes.
When they returned, Safina held to lead 2-0, but Sharapova then ran away with the next five games to lead 5-2 before her 22-year-old opponent mounted an astonishing recovery.
Safina saved a match point in the ninth game before pulling level at 5-5.
There was another dramatic twist in the tie-break where the battling Safina came back from 2/5 down to claim the next five points and level the match when a Sharapova approach took a net cord and went out.
After breaking each other in the opening two games of the decider, Safina held her nerve on a fourth break point in the sixth game to lead 4-2 and backed it up to go 5-2.
A shellshocked Sharapova saved a match point in the next game but then handed Safina a famous win when she hit a dispirited forehand long.
Read moreAll news
On 4 October, BNP Paribas Corporate and Institutional Banking organizes the Sustainable Future...
Whether it be for the circular economy, workforce integration or services for people with...