Earlier, Adam Peaty ended 28 years of Olympic hurt for Britain's male swimmers as he romped to gold in the 100 metres breaststroke final in Rio, smashing the world record for the second time in two days.
Cheered on by fiancee Nicole and toddler Boomer in the stands, the peerless Phelps again provided the spark, producing a blistering 47.12-second leg to overtake France's Fabien Gilot and set up victory in the 4x100m free.
"It was crazy," said Phelps, competing in his fifth and final Games. "I was standing on the block while Caeleb (Dressel) was coming in and I honestly thought my heart was going to explode out of my chest.
"We wanted that race back so badly," added Phelps, who hugged team mate Ryan Held on the podium as he sobbed tears of joy.
Also Read
"They were making us cry. The younger guys started crying, I started crying. My last 400 free relay ever, it feels damn good to get a win."
That paled compared to the boos that greeted the Russian men's 4x100m freestyle relay team, however, as fans and athletes supporting team mates vented their irritation after months of doping scandals.
World champion Peaty clocked 57.13 seconds, slicing
almost half a second off the mark he set in Saturday's heats to emulate Adrian Moorhouse's victory in the same event at the 1988 Seoul Games -- the last time a British male swimmer won gold.
"I saw those (British) flags and each one represented what I'm actually here for -- to represent one of the best countries on earth," said Peaty. "Even though we're a small country, we can still pack a punch... Each day, day in day out, I just gave it absolutely everything."
Ledecky, chasing a historic Rio treble in the 200, 400 and 800m free, produced another remarkable swim in the 400m, stopping the clock in an eye-popping 3:56.46.
Her time was her 12th world record and was almost five seconds quicker than silver medallist Jazz Carlin of Britain.
"I'd been so close to breaking that for the past two years and I knew I was due for a breakthrough."
No Swedish woman had ever won an Olympic swimming gold, but Sjostrom ended that streak with aplomb.
"It's the heaviest medal I ever won," she said after completing a collection of European, world and Olympic titles.
"I knew before the Olympic games that something was missing, I knew no other girls had won an Olympic medal before and I thought maybe it's time.