Cave also hit a sacrifice fly as the Phillies ended a three-game losing streak while gaining a split of the two-game set.
Daulton Varsho hit a two-run homer for the Blue Jays.
Toronto right-hander Kevin Gausman (9-7) allowed seven runs, five earned), on seven hits and three walks in 5 1/3 innings. He struck out four.
Philadelphia right-hander Aaron Nola (10-8) gave up four runs, four hits and four walks in five innings. He fanned seven.
The Phillies scored once in the second after loading the bases with no outs. Nick Castellanos led off with a double but only advanced to third on a double to left by Bryson Stott. Trea Turner walked before Cave hit a sacrifice fly to left.
Toronto took a 2-1 lead in the bottom of the second. Cavan Biggio singled and Varsho hit his 15th homer of the season, a shot to right.
Harper homered on a drive to center with two outs in the third to tie the game.
Toronto regained the lead with two runs in the bottom of the third. Brandon Belt walked with one out, and Vladimir Guerrero Jr. singled. George Springer hit a fielder's-choice grounder to third and stole second. Biggio followed with a two-run single for a 4-2 edge.
The Phillies took a 5-4 lead in the fifth. Kyle Schwarber and Harper walked, and Castellanos hit an RBI double with two outs. Santiago Espinal was charged with a throwing error on Stott's grounder to third, a play that saw two runs score. Espinal was playing third because Matt Chapman (right middle finger inflammation) missed his third straight game.
J.T. Realmuto led off Philadelphia's two-run sixth with a double and took third on Cave's infield hit. Tim Mayza replaced Gausman, and Schwarber hit a sacrifice fly and Alec Bohm added an RBI single.
Cave led off the eighth with his third homer of the season, a blast to center against Jay Jackson.
Harper led off the ninth with his ninth homer of the season, a 404-foot shot to right-center off Bowden Francis. It was his first multi-homer game of the season.
Philadelphia's bullpen allowed one run in four scoreless innings. Matt Strahm pitched the sixth and seventh, Craig Kimbrel handled the eighth and Gregory Soto tossed a 1-2-3 ninth.
—Field Level Media