It was the longest game of the season for the Phillies, who hadn't played 15 innings since Aug. 8, 2014.
Philadelphia improved to 7-9 in extra innings; the Marlins fell to 5-5.
Williams' hit scored Aaron Altherr, who singled with one out.
Edubray Ramos (2-7) picked up the win after pitching two scoreless innings. Javy Guerra (1-1) took the loss.
Miami is no stranger to walk-off losses. The Marlins arrived in Philadelphia having played a four-game weekend series in Atlanta, where the Braves won three times on their final at-bats.
Miami hast lost 13 of its past 15 games overall.
Once again, the Marlins' bullpen couldn't get outs when it needed them most.
The Phillies trailed 7-2 after six innings and 7-3 after seven before mounting a rally in the eighth off Marlins relievers Dustin McGowan and Drew Steckenrider to get within 7-6.
Kyle Barraclough came in to pitch the ninth inning for Miami and promptly hit Williams, who made it to third base a batter later on a single by Maikel Franco. A walk to J.P. Crawford loaded the bases with one out for pinch hitter Hyun Soo Kim, who delivered a game-tying single past a diving Dee Gordon at second base.
It looked as if the Phillies won the game before a two-minute review ruled that Cesar Hernandez -- pinch-running for Franco -- had been thrown out at home, allowing Barraclough to get out of the inning with a strikeout of Phillies center fielder Odubel Herrera.
Marcell Ozuna put Miami up 8-7 in the 10th inning with his 33rd home run of the year, but the Phillies' Rhys Hoskins tied it with a shot to straightaway center in the bottom half.
For Hoskins, the Phillies' rookie sensation, it was his second homer of the game and his 16th in 32 contests since being called up. No player in major league history has hit that many home runs in his first 40 games.
Herrera helped the Phillies take a 1-0 lead in the first inning, singling and stealing second base before an Altherr single drove him home.
The Marlins tied it up in the second on an RBI double by J.T. Realmuto.
Philadelphia briefly took a 2-1 lead in the third, when Miami starter Dillon Peters walked two batters to load the bases before hitting Williams to force in a run.
The Marlins took a 5-2 lead with four in the fifth inning. Derek Dietrich belted a 3-2 pitch into the stands in right field, scoring Realmuto after the catcher had walked to lead off the inning. It was Dietrich's 11th home run of the season and second in as many games.
The Marlins added two more in the fifth inning off Phillies starter Nick Pivetta, on an RBI single by Gordon and sacrifice fly from Christian Yelich.
Pivetta faced two batters into the sixth inning without recording an out, pulled after giving up an RBI double to Realmuto. The rookie right-hander was tagged for seven runs and eight hits, and he struck out four while walking one.
It was the fifth start this season in which Pivetta gave up at least six earned runs, part of the reason he sports a 6.75 ERA.
Peters, also a rookie, allowed two runs on six hits in six innings.
NOTES: Wednesday night's game pits Philadelphia RHP Aaron Nola (10-10, 3.71 ERA) against Miami RHP Dan Straily (9-8, 3.95) in a rematch of a 10-9 Marlins victory Sept. 2. ... Marlins 2B Dee Gordon extended his hitting streak to a career-high 15 games with an RBI single in the fifth inning. ... This was the first of 10 straight home games for the Phillies, who close with 16 of 19 games at home. No other team had played as few home games through Sept. 12 as the Phillies (66).