The 30-year-old Sharapova, making her comeback from a 15-month suspension for testing positive for the banned heart drug meldonium last year, was beaten by Canada's Bouchard, 7-5, 2-6, 6-4, in the second round of the Madrid Open.
The 60th-ranked Bouchard screamed and jumped up and down after she won her first match in five meetings against the five-time major winner who had once inspired her to become a tennis player.
If it had been billed as a grudge match it quickly morphed into one of the best contests of this season as both women refused to give each other an inch in a contest full of tension, spectacular baseline rallies and momentum swings.
Bouchard prevailed in the end, and their handshake at the net was cordial, with Sharapova saying "well played."
Although Bouchard has often said she has few friends on the women's circuit, she claimed afterward the locker room had been firmly on her side.
"I was inspired because I had a lot of players coming up to me privately, wishing me good luck," Bouchard said in a news conference, without naming anyone.
"They were players I don't normally speak to and I got a lot of texts from people in the tennis world that were just rooting for me. I wanted to do it for myself, but also for all these people."
Although Sharapova's comeback after serving her ban had split opinion among both male and female players, including second-ranked Angelique Kerber and top-ranked Andy Murray criticizing tournaments for giving her a wild card, Bouchard has been the most vocal.
"She's a cheater and I don't think a cheater in any sport should be allowed to play that sport again," Bouchard told Turkish broadcaster TRT World on April 25, the day before Sharapova made a highly-anticipated comeback to tennis at an event in Stuttgart, Germany, where she would eventually reach the semifinals.
In October, the Swiss Court of Arbitration of Sport reduced Sharapova's initial two-year ban to 15 months on appeal, saying she bore no "significant fault" and didn't intend to cheat.
Wild card controversy
Now ranked 258th, Sharapova is in need of wild cards to play events like Stuttgart, Madrid and Rome next week because she didn't have a ranking at the time of entry as she was still serving her suspension.
The French Open, which Shararapova has won twice, said it will take a decision on whether it will hand her a wild card on May 16.
Her defeat against Bouchard means Sharapova has to reach at least the semifinals of Rome to get straight into the main draw at Wimbledon in June.
Bouchard, who decided she wanted to become a tennis player when she watched Sharapova win Wimbledon in 2004, was also critical of the women's Tour.
"I think from the WTA it sends the wrong message to young kids: cheat and we'll welcome you back with open arms," Bouchard said.
"I don't think that's right and definitely not someone I can say I look up to any more because it's definitely ruined it for me a little bit."
When asked about Bouchard's comment two weeks ago in Stuttgart, Sharapova told reporters: "I don't have anything to say -- I am way above that."
On the clay of the Court Manola Santana in Madrid, Sharapova initially let her racket do the talking as she took a 4-2 lead in the first set before losing a highly-entertaining yet error-strewn 70-minute first set 7-5 as she struggled to get less than half of her first serves in and produced six double faults.
In the first set's seventh game, the former top-ranked Russian seemed to lose focus as she mistimed a drop shot at 30-30 and let her opponent back into the match by dropping three games in a row.
After Sharapova received a pep talk from her coach, Sven Groeneveld, who told her to "take a deep breath and get back to your basics," she broke straight back with aggressive tennis for 5-5.
But Bouchard broke once more in the next game and this time didn't waver as she served out the first set with a huge serve followed by a blistering forehand winner.
The match went to a decider as Sharapova quickly took the second set, breaking for 3-2 as Bouchard's level temporarily dipped.
There seemed to be some psychological warfare going on early on in the second set, as Bouchard turned and smiled after she fended off a backhand that Sharapova had loudly drilled at her from the baseline with a volley.
After the third set started with three consecutive service holds from 0-40, play was back on serve at 4-4 as both got broken.
Faced with yet another break point, Sharapova went for broke on a backhand but just missed the line. Serving for the match, Bouchard converted her second match point with a cross-court forehand, her 20th winner.
Although Sharapova produced 44 winners, her lack of matches in the past 15 months showed as she made 49 unforced errors, including nine double faults.
"I think I would be worried about myself if I sat here and said I'm pretty happy with losing a tennis match, no matter who I face, no matter what round it is, whether it's the first round or final of a grand slam," Sharapova said in a news conference after the match.
"Today was just not that day. Of course, I'm disappointed. That's what's going to make me a better player. That's what's going to win me more tournaments and more grand slams."
Bouchard's win is all the more remarkable because she hadn't managed to win a match in three-and-a-half months before she entered Madrid.
The 23-year-old Canadian had struggled with the weight of expectation after a breakthrough season in 2014, when she reached back-to-back semifinals at the Australian Open and Roland Garros before going all the way to the Wimbledon finals, where she was overwhelmed by Petra Kvitova.
Having finished the 2014 season inside the top 10, the media attention and extra pressure got to Bouchard in the years that followed as she struggled with a loss of form and confidence and saw her ranking plummet.
After parting ways with coach Thomas Hogstedt in March last year, Bouchard reunited with the Swede who used to guide Sharapova in December.
Although Bouchard started the season with a semifinals spot in Sydney and a third round at the Australian Open, she failed to win a single match until beating France's Alize Cornet in the previous round in Madrid and even played a second-tier ITF event last month in Florida to regain her confidence.
She now faces reigning US Open champion Kerber in the third round. Bouchard leads the German 3-2, including two victories on clay.