Ferran Jutgla headed in an opener for the hosts after 16 minutes before 17-year-old Gavi grabbed his first La Liga goal with a superb solo effort.
Substitutes Tete Morente and Pere Milla then stunned the hosts as Elche scored twice in as many minutes to draw level.
But Gonzalez poked into the top corner five minutes from time to win it.
It means Xavi’s side sit seventh, three points off a Champions League spot and 15 behind leaders Real Madrid.
They are now only two points behind fifth-placed champions Atletico Madrid, who lost 2-1 to Sevilla.
Barcelona had failed to win their previous three outings, with a 2-2 draw at Osasuna last Sunday following defeats by Bayern Munich in the Champions League and Real Betis in La Liga.
Jutgla saw an effort ruled out for offside inside 90 seconds but the summer signing did not have to wait long for his first goal at the Nou Camp, heading in Ousmane Dembele’s corner.
The impressive Gavi added a second three minutes later, flicking the ball through his legs on the turn and running directly at the defence before picking out the bottom corner
But Barcelona momentarily collapsed in the second half, conceding twice in two minutes as first Morente fired into the roof of the net and then Milla headed in at the back post after the hosts gave the ball away.
It was an inspired double change from boss Francisco Rodríguez, who saw the substitutes score within five minutes of being brought on.
Elche looked to have earned a hard-fought point, only for to ball to drop to Gonzalez, who finished smartly to earn Xavi’s young side all three points.
Elche remain 16th, two points above the relegation zone.
Second-placed Sevilla are five points behind leaders Real after beating Atletico.
This is the first time in Diego Simeone’s 10 years in charge that Atletico have lost three La Liga games in a row.
Lucas Ocampos scored the late winner after Thomas Delaney had headed against the bar.
Ivan Rakitic had given Sevilla an early lead, with Felipe equalising for Atleti.