It was Halep's first match since October after a lengthy layoff due to a herniated disc in her back. Halep won last year's French Open and lost to Caroline Wozniacki in the Australian Open final.
"I think it was a great match after almost four months since I played one full match, the U.S. Open," Halep said. "I think I played a good level of tennis. She played really well. Her slice was great today, so she deserved to win this match.
"But also I was very close to win. I had so many chances to break her and to take advantage, but I couldn't."
Halep said she was pain-free but would wait to see how she pulls up on Thursday before declaring that she is over the back injury.
"No pain at all. I'm happy about that. It's a great sign," she said. "I will recover now, and I will see how I wake up tomorrow morning."
Barty served five aces, no double-faults and broke Halep three times to reverse two previous losses to Halep last August.
"I finally learned my lesson," Barty said. "She gave me a belting in Montreal. I learned a little bit more in Cincinnati, but today I knuckled down and really thought about it and really gave myself a chance.
"The biggest thing, I believed that I could I win. We're both out here on the same court. Just very happy I was able to come out and execute."
The 27-year-old Halep will be at the Australian Open next week in Melbourne without a coach after parting ways with Australian mentor Darren Cahill, who can longer commit to Halep due to family reasons. Cahill had coached Halep for four years.
In first-round matches, 10th-seeded Elise Mertens beat qualifier Katerina Siniakova 7-6 (4), 2-6, 6-2 and Anett Kontaveit defeated Monica Puig 2-6, 7-5, 6-1.
In the second round, Timea Bacsinszky defeated Samantha Stosur 5-7, 6-4, 6-3 and Yulia Putintseva beat fourth-seeded Sloane Stephens 3-6, 7-6 (4), 6-0.
In men's play, Alex de Minaur beat American qualifier Reilly Opelka 6-4 7-6 (4) in a match suspended by rain after just one game on Tuesday night to advance to the quarterfinals against fellow Australian Jordan Thompson.
"I think I got lucky today. Reilly's an incredibly tough opponent," de Minaur said. "I took care of my chances. I would have loved to get the break in the second. I had a couple of chances, but I did well to focus on my serve and I'm glad things went my way today."
In other second-round matches, top-seeded Stefanos Tsitsipas defeated Guido Andreozzi 6-3, 6-4, third-seeded Diego Schwartzman beat qualifier Guillermo Garcia-Lopez 6-2, 6-3, and fourth-seeded Gilles Simon defeated Sam Querrey 7-6 (5), 6-7 (6), 6-4.
More AP Tennis: https://www.apnews.com/apf-Tennis and https://twitter.com/AP_Sports