Malinga returned to take 2-12 in his second spell, claiming his 50th World Cup wicket when he had Joe Root (57) caught behind edging down the leg side. He made it 51 when he trapped Jos Buttler (10) lbw with a full ball that crashed into the batsman's boot.
The paceman became just the fourth bowler to take more than 50 wickets at the World Cup, joining retired greats Glenn McGrath (71), Muttiah Muralitharan (68) and Wasim Akram (55).
He almost bagged a five-wicket haul, but Ben Stokes was dropped by Kusal Mendis at deep midwicket on his penultimate delivery. Malinga finished with figures of 4-43 off 10 overs.
Stokes responded by going on the attack, with nine wickets down, stroking two sixes and two boundaries. He tried to retain the strike as much as possible, declining singles, but was left stranded on 82 when N. 11 batsman Wood was caught behind at the end of the 47th over.
Offspinner Dhananjaya de Silva's 3-3 in nine balls triggered the late collapse, starting when Moeen Ali (16) was caught on the long-on boundary in the 39th over.
Sri Lanka's win shakes up a tournament that was being predictable.
For England, it was a defeat that seemed to come out of nowhere, but was similar to the letdown when it was beaten by Pakistan after winning the tournament opener so emphatically against South Africa.
England responded to that loss to Pakistan by beating Bangladesh, West Indies and Afghanistan and was confident of overhauling Australia for top spot in the standings before their match against a struggling Sri Lanka.
"The experienced guys had to come into the game. Unfortunately, we couldn't win the last two games but we managed to scrap today ... fantastic," Mathews said. "We had to play our best cricket to beat the best team. Throughout the competition, they've been running over sides. We had to come over something extraordinary, which we did today."
Sri Lanka had only one win — over Afghanistan — two washed out games, and two losses coming into Headingley. After being on the brink of missing the semifinals, the Sri Lankans have six points and renewed hope.
There was a positive for England, with Archer returning 3-52, his fifth three-wicket haul in six games, to equal Australia paceman Mitchell Starc at the top of the tournament's bowling list with 15.
Fellow fast bowler Mark Wood took 3-40 to improve his tally to 12, and wrist spinner Adil Rashid took two wickets with consecutive balls in a haul of 2-45.
The Sri Lankans had three 50-plus partnerships but didn't consolidate, and twice lost wickets in pairs, relying on Mathews' 115-ball dig to get them to a defendable total.
Avishka Fernando scored 49 from 39 balls and was within a shot of his half-century when he guided a ball from Wood to third man to end a 59-run third-wicket stand.
That brought Mathews to the crease and he shared a 71-run partnership with Mendis, who scored a patient 46 from 68 balls before Rashid struck twice in two deliveries.
England needs to rebound quickly for its game against Australia at Lord's next Tuesday, while Sri Lanka gets a break before taking on South Africa in Durham next Friday.
- AP