Despite being unable to send down serves of any power Williams raced into a four-game lead before Sharapova found her groove to win six games in a row.
The American's injury time out knocked Sharapova out of her stride.
Williams edged a tight first set with a late break of serve and, after taking another early in the second set, it seemed as if the result would be a formality.
But Sharapova battled back to level with some flourishing groundstrokes but could not maintain that form early in the deciding set.
Williams could muster little more than a lobbed serve at times, but at 4-0 down Sharapova suddenly refound her form and raced to victory.
"AMAZING YEAR"
"I'm still in shock. I can't believe the way I pulled it out," said the 17-year-old Russian, who fell to the ground and screamed with joy at the end of a contest lasting one hour 46 minutes.
"It's very unreal the way I stuck in there. It's been an amazing year."
Williams said she felt the injury come on in the opening game of the match.
"I really thought it was just a stitch, and it would go away," Serena said. "I thought at times I wouldn't finish, but I like to fight. On a 0-10 scale, the pain was a 10 and a half."
"It's extremely disappointing. I figured I had a good chance at this title."
Serena recalled the 2003 Wimbledon semi-final when her sister Venus continued to play against Kim Clijsters despite an abdominal strain and eventually tore it in the match. Venus did play the final where she lost to Serena, but she was then off the tour for the next six months.
"I was thinking I'm not going to be out six months," Serena said. "I wasn't going to go for any big serves because it's not worth it with the New Year coming around."
"She was hitting as hard as she could and everything was going in," Sharapova said.
"She was tough. I couldn't capitalize on the weak serves. I was just trying to find a little opening. I was just mentally strong."
POSSIBLE ABDOMINAL TEAR
Sharapova, who now moves to a career-high number four in the world rankings, also became the first champion from Russia and only the second player to win the title on her debut in the event. Serena Williams won it on her first appearance in 2001.
Williams said the injury might be an abdominal tear and she plans to have an MRI scan on Tuesday.
An unfortunate draw, however, means he must defeat world number one Roger Federer in the quarter-finals at the Rothenbaum on Friday if he is to end his losing streak.
Federer also beat Coria in the Hamburg Masters final last year - surprisingly their only previous meeting -- and the Argentine admitted: "It will be difficult. It's going to be similar to how it was against Nadal. I have to take one point at a time.
"They both play complete tennis and I am going to have to be at my very best to win."
The 23-year-old clay specialist, who will be a big threat at the French Open which starts on May 23, is in danger of being known as a perpetual runner-up.
He also lost in the final at Roland Garros last year and those two defeats by Nadal in Monte Carlo and Rome would have deflated lesser men.
Coria, though, said the defeat in Rome on Sunday, when he lost a five-set, five-and-a-quarter-hour marathon, had been a source of inspiration after a difficult 2004 in which he spent three months recovering from shoulder surgery.
"I'm very happy to have got back the confidence that I had lost," said the 10th seed after his 6-4 6-3 third round win over Croatian Mario Ancic on Thursday.
"The final in Rome has given me a lot of confidence. Even though I lost, I played some great tennis. It was a key match.
"I was out of the game for three months. I did a lot of physical work and in the long matches that really helps with your frame of mind.
"This tournament was very important for me to see how I was physically, and it's good to be playing all these matches."
Coria, who will be seeking his first grand slam title at Roland Garros, will be the first player to reach the final of all three Masters Series claycourt events in one season if he makes the Hamburg final.
An unfortunate draw, however, means he must defeat world number one Roger Federer in the quarter-finals at the Rothenbaum on Friday if he is to end his losing streak.
Federer also beat Coria in the Hamburg Masters final last year - surprisingly their only previous meeting -- and the Argentine admitted: "It will be difficult. It's going to be similar to how it was against Nadal. I have to take one point at a time.
"They both play complete tennis and I am going to have to be at my very best to win."
The 23-year-old clay specialist, who will be a big threat at the French Open which starts on May 23, is in danger of being known as a perpetual runner-up.
He also lost in the final at Roland Garros last year and those two defeats by Nadal in Monte Carlo and Rome would have deflated lesser men.
Coria, though, said the defeat in Rome on Sunday, when he lost a five-set, five-and-a-quarter-hour marathon, had been a source of inspiration after a difficult 2004 in which he spent three months recovering from shoulder surgery.
"I'm very happy to have got back the confidence that I had lost," said the 10th seed after his 6-4 6-3 third round win over Croatian Mario Ancic on Thursday.
"The final in Rome has given me a lot of confidence. Even though I lost, I played some great tennis. It was a key match.
"I was out of the game for three months. I did a lot of physical work and in the long matches that really helps with your frame of mind.
"This tournament was very important for me to see how I was physically, and it's good to be playing all these matches."
Coria, who will be seeking his first grand slam title at Roland Garros, will be the first player to reach the final of all three Masters Series claycourt events in one season if he makes the Hamburg final.
