The 30-year-old Russian, who had only one hardcourt tune-up match due to a nagging forearm injury, swatted 60 winners with 64 unforced errors, setting the tempo and baffling Halep at times in a tension-packed thriller at Arthur Ashe Stadium.
"I just thought this was another day, another opportunity, another match but this was so much more," Sharapova said.
"You sometimes wonder why you put in all the work and this is exactly why."
Five-time Grand Slam champion Sharapova closed her remarks to a supportive crowd by saying that behind her fancy dress and sparkly crystals, "This girl has a lot of grit and she's not going anywhere."
Sharapova, who tested positive for the banned blood booster meldonium at last year's Australian Open, improved to 7-0 in her all-time rivalry with Halep, extending her mastery over the 25-year-old Romanian.
An emotional Sharapova punctuated her shotmaking, at times erratic and other moments spectacular, with screams and fist pumps. She was energetic even at rest, closing her eyes and bouncing her legs as she sat between sets.
After the final point, Sharapova dropped to her knees as a replay appeal showed what she already knew.
She rose and greeted Halep at the net, thanked the umpire, then battled back tears as she blew kisses to spectators, sobbed at times and mouthed "Thank you" to fans that were devoted throughout the two hour and 45 minute drama.
"You never know what you're going to feel until you win that match point," she said. "It was so worth it."
Sharapova won only 5 of 22 break points in the match while Halep won 4 of 10 and produced only 15 winners against 14 unforced errors.
It was Sharapova's first Grand Slam match since a quarter-final loss to Serena Williams in last year's Australian Open.
Sharapova, whose major titles include the 2006 US Open, advanced to a second-round matchup against Hungarian Timea Babos, whom she has never played.
Sharapova, ranked 146th, returned in April but the French Open snubbed Sharapova for a wildcard entry and she missed Wimbledon with a thigh injury.
Asked about her low points, Sharapova, said, "There were definitely a few but I don't think this is the time to talk about that."
Sharapova walked onto the court at Arthur Ashe Stadium to rousing applause from a clearly supportive crowd.
Sharapova wore a black dress, visor, socks and shoes, the same style she sported in taking the trophy nine years ago on the New York hardcourts.
Two early exchanges of breaks left the first set level at 4-4 before Sharapova held and broke Halep with a forehand winner to draw first blood after an hour.
In the second set, Sharapova double faulted away a break to hand Halep a 5-4 edge, then Halep saved three break points and held to force a third set.
Halep sent a backhand beyond the baseline to hand Sharapova a break and 2-0 lead in the final set. The Russian held to 5-3 and served for the match, escaping a break point when Halep netted a forehand.
A Sharapova backhand winner to set up match point bought huge applause but the truly thunderous ovation came when Halep hit a forehand long to end it, boosting Sharapova to 18-0 in night matches at Ashe.
"It's prime time baby," she said. "I love it."
Reigning Wimbledon champion Muguruza advanced and British seventh seed Johanna Konta was ousted in other women's matches.
Spanish third seed Muguruza defeated American Varvara Lepchenko 6-0, 6-3. Muguruza, last year's French Open champion, can reach the US Open third round for the first time Wednesday by defeating China's Duan Ying-Ying.
"Coming to US Open and having a dark past in the results, I keep it with low expectations," Muguruza said.
Serbia's 78th-ranked Aleksandra Krunic upset Australian- born Konta 4-6, 6-3, 6-4.
"It's definitely not an easy loss to take," Konta said. "She played consistently much better than I did."
Croatian fifth seed Marin Cilic, the 2014 US Open champion and last month's Wimbledon runner-up, ousted 105th- ranked American Tennys Sandgren 6-4, 6-3, 3-6, 6-3.
"I feel just a little bit rusty," Cilic said. "I need a few matches to get into the rhythm."
Seven-time Grand Slam champion Venus Williams, this year's Australian Open and Wimbledon runner-up, outlasted Slovakia's 135th-ranked Viktoria Kuzmova 6-3 3-6 6-2.