The defending champions got off to the perfect start as Lionel Messi picked out Philippe Coutinho, who volleyed into the back of the net from 25-yards out.
Barca continued to push for a second goal and were nearly rewarded in the 16th minute when Lionel Messi rattled the crossbar with a strike from the edge of the box.
Leganes had to wait until the 30th minute for their first opening as Youssef En-Neysri broke into the box and picked out Rubén Pérez, who fired straight at goalkeeper Marc-Andre Ter Stegen.
The Barca shot-stopper was called into action minutes before the break as he produced a good save to keep out Óscar Rodríguez's header.
Former Liverpool winger Nabil El Zhar restored parity for the hosts in the 52nd minute, heading home Jonathan Silva's cross after Thomas Vermaelen went to sleep.
Three minutes later, the turnaround was complete through Rodriguez, who profited from a defensive error from Pique to slot past Ter Stegen to put the hosts ahead.
Barca pushed for an equaliser but despite dominating the possession, Ernesto Valverde's charges struggled to carve out chances in the final third.
Coutinho almost pulled Barca level with 10 minutes remaining in the half but goalkeeper Ivan Cuellar came up with the big save, while also denying Ivan Rakitic on the rebound.
In the end, Leganes hung on to claim three points and handed Barca their first defeat of the season.