The Victoriaville, Que. native scored 47 points on his best run and 44 on his second best for a combined top score of 91 points.
"I did two tricks that I never tried before and they both worked out," said Gagnier. "Since 1999 when I first watched the X Games, I've been wanting to do this. It's awesome, it is everything. It is the biggest contest in skiing. The ultimate goal. The Olympics are not the goal, this is it."
Bobby Brown of the United States finished second with 82 points and Switzerland's Elias Ambuhl took bronze.
Earlier, Olympic gold medallist Dara Howell of Huntsville, Ont., won bronze in slopestyle skiing.
Howell scored 82.00 points on her third run to win the third X Games medal of her career, complimenting her bronze-medal finishes from 2013 and 2012. Sweden's Emma Dahltstrom took gold with 90.33 points while American Keri Herman placed second with 86.66.
"My skiing was good today even though I didn't have the greatest sets of training (before the competition)," said Howell. "I think I put a lot of pressure on myself. I went into practice this morning with an open mind and it started to click. It felt like gold to me to get that third-place medal."
Nikki Blackall of Barrie, Ont., finished fifth while Anouk Purnell-Faniel of Quebec City came in sixth.
On the men's side, Alex Bellemare of Saint-Boniface, Que., took bronze with 85.66 points. American Nick Goepper finished first with 93.66 and his teammate, Joss Christensen, placed second with 90.66.
Bellamare's podium finish marked the biggest win of his career and came in only his second X Games. He placed fourth at his only other appearance in 2013.
"It was such a relief when I won bronze," said Bellemare, who missed last year's competition due to injury. "I knew I was in third place and I just had to wait it out until everyone finished their run. I'm thrilled with how things turned out. I was really focused on my rails today. They are my specialty and I nailed them.
"I had a good day overall and am really happy with my performance. It's my best result to date."
Quebec City's Alex Beaulieu-Marchand was scheduled to compete in the eight-man final, but pulled out due to injury. He finished ninth overall. Evan McEachran of Oakville, Ont., the youngest member of the Canadian slopestyle team at 17-years-old, finished in 14th place.Suggest a correction