Elche took a surprise lead at the Martinez Valero when Fidel capitalised on a Pedri error on the stroke of half-time but Barca came storming back thanks to two substitutes.
Ferran Torres diverted in from close range on the hour before Depay scored an 84th-minute penalty after the striker had drawn Elche's Antonio Barragan into a handball.
"It seemed we were going to lose a game we did not deserve to lose," said Barca coach Xavi Hernandez. "We deserved to win by more than one goal."
The victory sends Barcelona two points above Real Betis and three clear of Atletico Madrid in the table before Betis and Atletico face each other in a crucial battle for the top four later on Sunday.
It also continues an impressive run of form for Xavi's side, who have not lost in the league since December and came into this game on the back of scoring four times in three consecutive games for the first time since 2017.
When Xavi was appointed in November, Barcelona sat ninth but they might now justifiably have their sights set on Sevilla, who are only seven points ahead in second.
"We have four wins in a row, this has not happened for a long time, it's a good sign," said Xavi. "The players believe in what we are doing. We are on the right path."
Barcelona had the better of the chances in the first half, the best of them falling to Frenkie de Jong, who darted in behind but failed to beat Edgar Badia.
Then Ousmane Dembele sprinted clear and his lifted shot was parried out to De Jong, whose follow-up was cleared off the line by Diego Gonzalez.
Elche then landed a sucker-punch a minute before half-time as Barca failed to deal with a straight-forward goal-kick downfield and amid the confusion, Pedri poked the ball back, past his own defence and through to Fidel. Fidel took a touch and fired into the far corner.
Barcelona equalised on the hour as they worked the ball from left to right, where Dembele drove to the line and hung up a cross. Jordi Alba was unable to catch the ball cleanly but Torres was perfectly placed to steer in.
Elche were tiring, Barca pushing for a winner and in the 82nd minute they got their chance as Depay wriggled around Barragan, the ball popped up and Barragan instinctively lifted his arm to meet it.
The referee consulted the replay, pointed to the spot and Depay swept the penalty into the top corner.
There was more late drama to come, as Alba and Sergio Busquets were both relieved to see penalty claims for handballs waved away before Guido Carrillo headed towards the corner in the 94th minute. Marc-Andre ter Stegen dived and Barca held on.
Barca now turn attention to Europa League on Thursday against Turkish side Galatarsaray.