Sean Dyche's side had lost on 13 of their 15 Premier League visits to London and had won just one of their last 11 league matches to slip precariously close to the relegation zone.
But a vital three points sees Burnley move away from danger - the Clarets are eight points clear of third-bottom Swansea City - and they now look well set to survive in the Premier League for the first time in the club's history, at the third attempt.
Barnes, who also netted the winner when the sides met earlier in the season, gave Burnley the lead after seven minutes, the striker scoring from Stephen Ward's low centre after the visitors had survived sustained early Palace pressure.
PIC: Ashley Barnes puts the Clarets ahead in the game at Selhurst Park. 0-1 pic.twitter.com/ooQHRnyBkl— Burnley FC (@BurnleyOfficial) April 29, 2017
Without injured defenders Mamadou Sakho, Scott Dann and James Tomkins, Palace struggled to cope with Burnley's aerial threat - although Damien Delaney forced a superb save from Tom Heaton at the other end. And the visitors, without Joey Barton for the first time following the 18-month betting ban handed out in midweek, wrapped up the points when Gray finished off a break with five minutes to go.
Burnley's win moves them above Palace, with the teams left eight and seven points clear of the relegation zone respectively, although Swansea in 18th have an extra game to play.
Palace won four corners in the first two minutes and Heaton pushed away Christian Benteke's strike as the hosts made a positive start.
But Burnley promptly scored with their first attack when Sam Vokes and Ashley Westwood combined well to feed Ward, the left-back delivering a low cross into Barnes.
The striker mishit his first-time effort, but reacted quickly to rifle home through the legs of Palace goalkeeper Wayne Hennessey for his sixth goal of the season, then defender James Tarkowski appeared to be struck by an object thrown by a Palace fan as his team-mates celebrated.
85 - GOOOOALLLLLLL pic.twitter.com/fCqMowzyJr— Burnley FC (@BurnleyOfficial) April 29, 2017
Barnes had the ball in the net again after 15 minutes after latching on to Michael Keane's header, but the forward controlled the ball on his arm and the goal was disallowed.
Luka Milivojevic hammered wide from yet another corner, but Burnley continued to threaten Palace's patched-up defence and Hennessey made a fine save to keep out a firm Vokes header.
Milivojevic tested Heaton with a long-range effort after 54 minutes, but the visitors were still holding Palace at bay relatively easily until Delaney let fly.
Heaton had to leap to his right at full stretch to claw the defender's effort over the crossbar to protect his clean sheet, before Sam Allardyce threw on strikers Loic Remy and Fraizer Campbell with 20 minutes to go.
Wilfried Zaha's penalty appeals were rejected after substitute Jon Flanagan clumsily tackled him from behind and Burnley added a late second on the break, Gray racing on to George Boyd's pass to smash home a low finish to secure three massive points.
Key Opta stats:
- Burnley became the last Premier League side to finally secure an away win this season, at the 18th time of asking.
- After a run of three consecutive home league wins, Crystal Palace have failed to win any of their last three at Selhurst Park, with a draw followed by consecutive defeats.
- Stephen Ward registered his first Premier League assist for Burnley, and his first in the competition since May 2012 for Wolves v Wigan.
- Andre Gray netted his first goal in his last eight Premier League games after bagging seven in the 10 before that.