Lionel Messi missed a second-half penalty before Melendo appeared with a spectacular moment late on to ensure that Espanyol would take a lead into the second leg at Camp Nou next week.
Espanyol's team showed five changes from the one that started against Athletic Bilbao in the league at the weekend. Diego Lopez replaced Pau Lopez between the sticks, while Marc Navarro, Naldo, David Lopez and Javi Fuego came in for Mario Hermoso, Javi Lopez, Jose Jurado and Leo Baptistao.
As for Barcelona, head coach Ernesto Valverde made five changes to the team that started against Real Sociedad. Jasper Cillessen came in for Marc-Andre ter Stegen between the sticks, while Lucas Digne, Carles Alena, Denis Suarez and Aleix Vidal also started for the visitors.
It was Sergio Busquets that had the game's first attempt in the eighth minute, but the Barcelona midfielder saw his low effort pushed wide of the post by Diego Lopez.
Messi had the opportunity to threaten from a couple of free kicks in the first 20 minutes, but the Argentine could not take advantage of some dangerous positions outside the Espanyol box.
Messi's first strike on target came in the 22nd minute after finding space in a dangerous area, although it was a fairly routine save for Diego Lopez between the sticks.
Gerard Moreno then had a decent chance for Espanyol just before the 30-minute mark, but the striker missed the post from a narrow angle as the score stayed goalless in Catalonia.
Denis Suarez had easily the best chance of the first half having been fed by Alena in a dangerous position just before the break, but the Spaniard put the ball wide of the near post.
Baptistao came off the bench at the interval as Espanyol boss Quique Flores looked to provide support for Moreno in the final third of the field.
Barcelona had the first chance of the second period when Messi floated a deep free kick towards the head of Thomas Vermaelen, but Naldo came from nowhere to make a brilliant clearance.
Espanyol full-back Aaron Martin, who had already been booked, was extremely fortunate to stay on the field in the 53rd minute, meanwhile, having brought Sergi Roberto to the ground in a dangerous area.
Luis Suarez came off the Barcelona bench just before the hour as Valverde looked to secure an advantage ahead of the second leg at Camp Nou next week.
Messi had the chance to score from the penalty spot in the 62nd minute after Roberto was brought to the ground by Esteban Granero inside the box, but Espanyol stopper Diego Lopez made a wonderful save.
Rafinha came off the Barcelona bench for his first appearance of the season 12 minutes from time - moments after Cillessen appeared to be struck with an object thrown from the crowd.
Cillessen had to keep out a brilliant free kick from Navarro in the 83rd minute as Espanyol pushed for a winner, but the hosts did take the lead in the 88th minute when 20-year-old Melendo slotted a low Navarro cross into the back of the net.
The goal sparked jubilant scenes inside the stadium as Espanyol celebrated a famous win over their rivals.
ESPANYOL (5-4-1): Diego Lopez; Navarro, David Lopez, Duarte, Naldo, Aaron (Vila 58'); Granero, Fuego (Melendo 75'), Sanchez, Darder (Baptistao 46'); Moreno
BARCELONA (4-3-3): Cillessen; Roberto, Pique, Vermaelen, Digne; Busquets, Alena (L.Suarez 59'), Paulinho (Rakitic 54'); D.Suarez (Rafinha 78'), Messi, Vidal