After working his way up from fifth on the grid, Bastianini made his way up to Bagnaia at the front before putting pressure on his fellow Italian.
The drama arrived on lap 21 as Bastianini overtook Bagnaia before the latter immediately took his position back, only to hit the kicker and run wide.
That allowed Bastianini to ease back into the lead, with Bagnaia's race-ending after going into the gravel shortly after.
"I'm really happy about this race. The weekend has been really complicated for me with some crashes," Bastianini said after his win.
"In the race I see that my pace was really nice, and at the end when I see Pecco [Bagnaia] very close I understand 'okay now you have to stay behind', and I tried overtaking him in the second corner to make him a little bit nervous, and at the end he goes long and I win this race.
"It's all for my team because they work a lot at this Grand Prix to give me the best bike, and it's incredible this job."
Bagnaia started on pole alongside fellow Ducati rider Jack Miller, who did at least finish second, and the Australian explained why he had allowed Bagnaia to get back ahead of him early in the race.
"The boys rode a strong race at the front there. Pecco wanted to pass me early on, it seemed like he had a bit better speed," Miller said. "I had good grip, I just wasn’t able to push off on that right-hand side.
"Pecco wanted to pull away so I was like 'alright, go for it', but as soon as he got in the front he sort of started having moment after moment.
"Then I saw Bastianini coming and coming. He put the move on me and to be honest I had nothing back for him until he and Pecco got into a bit of a battle, my lap times started coming down again there towards the end and I was actually able to come a bit closer to him, but yeah he was riding really well today."