The top-seeded Sharapova committed two consecutive double-faults to drop her serve at the start of the decisive set. But Sharapova won the last eight points and clinched the match when her third-seeded German opponent hit a backhand long.
"I like it quick and easier, but the great thing is I got through," she said.
Sharapova was pushed to three sets for the third straight match in Stuttgart and missed a chance to serve out the match after taking a 5-3 lead.
Li broke serve for 2-1 in second set, only to lose the advantage in the next game with a double-fault. But Mattek-Sands dropped her serve in the seventh game and double-faulted on Li's second match point.
Li became the first Asian player to win a Grand Slam singles title when she became the French Open champion in 2011.
"The match was very tight, she played good tennis," Li said.
Li has never beaten Sharapova in five previous matches.
After winning in Stuttgart last year, Sharapova went on to take the French Open title to complete a career Grand Slam. But she wouldn't draw parallels between the two seasons.
"It's too early to talk about the French Open and I don't like to compare different years. You play different players, different conditions, but you can build confidence if you have success on the same court," the Russian said.
Sharapova, ranked No. 2, is aiming for her second title of the year after winning at Indian Wells. She would be the first player to defend her title in Stuttgart since Lindsay Davenport in 2005. Sharapova has 15 straight wins on clay.
The Russian set the tone in the first set by breaking serve in the opening game. Kerber saved two set points, but then double-faulted. She rallied in the second, twice breaking Sharapova's serve with aggressive play.
"I lost momentum by not being aggressive," Sharapova said.
Both players mixed sizzling winners on the lines with errors. Sharapova was particularly inconsistent, hitting 42 winners but also producing 44 unforced errors.
But her strong finish in the last two games allowed Sharapova to improve to 4-1 against the sixth-ranked Kerber.
"I came through in the end, I did my job," Sharapova said. "I am pleased how I fought in the third."
Kerber said she'd given it her best shot.
"In the end, she played very well, she hit the ball well and she was confident," Kerber said. "She played the big points incredibly well. That's why she is No. 2 in the world."