"It was a very close match. I know she is a fighter and it would be difficult until the end," sixth seed Garcia, who saved two match points in the second set, said.
Sharapova, who came back from a 15-month doping ban at this tournament last April, won the first set with ease but the 31-year-old could not prevent a determined Garcia from dragging herself back into the contest through a tiebreak.
A 10th double fault by Sharapova at 4-4 handed Garcia the crucial break in the decider and the world number seven clinched the contest on her serve in the next game.
"I was a little more aggressive today and I returned a bit better. Finally I can say that after seven years I have beaten her... it's an important win," Garcia added.
Up next for Garcia is Ukrainian teenager Marta Kostyuk who beat Antonia Lottner 6-4 6-1. The 15-year-old qualified for the main draw by overcoming France's Alize Cornet on Monday.
"I can't say I'm super surprised, because that's what I work for, and I know I can play at this level," Kostyuk said.
World number six Karolina Pliskova also advanced with a 6-2 6-2 win over Kiki Bertens while Magdalena Rybarikova dumped out Russia's Daria Kasatkina 6-2 6-2.
(Reporting by Shrivathsa Sridhar in Bengaluru; Editing by Christian Radnedge)
