Barnes struck after just eight minutes and Tom Heaton kept Palace at bay before substitute Gray hit a late second to ensure the Clarets finally registered their first success on the road this season.
It was also Burnley's first ever Premier League victory in London - at the 16th attempt - which lifted them above Palace and, crucially, eight points clear of the drop zone.
Unfortunately there was an unsavoury incident as Burnley celebrated their first goal, when defender James Tarkowski appeared to be struck by a lighter thrown from the home crowd.
But nothing was going to take the shine off Burnley's win, which saw them take a huge step towards securing their place in the Premier League.
Palace should also be fine in truth, although manager Sam Allardyce knows the job is not done until they are mathematically safe and will not want to still be in the equation for much longer.
Palace actually started strongly, with Luka Milivojevic heading narrowly over from one of three corners in quick succession, before Christian Benteke's shot was beaten away by Heaton.
Yet from virtually their first foray into Palace's half, Burnley took the lead.
The ball was swept out to Stephen Ward on the left and his low cross found Barnes, who miskicked horribly with his first attempt but had time to stab his second through Wayne Hennessey's legs and into the net.
Burnley's players celebrated in front of the Holmesdale End, from where something was thrown which hit Tarkowski on the head.
Barnes had the ball in the net again after a quarter of an hour, but referee Bobby Madley took a long, hard look before deciding the striker had handled as he controlled Michael Keane's header.
Burnley had further chances to add to their lead but Sam Vokes glanced one header over the crossbar and saw another superbly saved by Hennessey at point-blank range.
Palace's best chance of the first half fell to Milivojevic, who lashed a loose ball from a corner inches wide.
But they almost hauled themselves level in spectacular fashion after the break when centre-half Damien Delaney, of all people, launched a stunning volley from the edge of the area only to see Heaton acrobatically claw it over the crossbar.
Palace were baying for a penalty when Jon Flanagan upended Wilfried Zaha in the area, but Madley said no.
And Gray, on the field for just six minutes, ensured Burnley would leave the capital with three points for a change when he raced on to George Boyd's pass and drilled home the killer second five minutes from the end.