Jonna Sundling, who won the sprint freestyle title to land Sweden the first gold medal of the championships, finished 10 seconds further back to win bronze.
On the second of the six laps of the course at the Granåsen Ski Centre, Norwegians Johaug, Heidi Weng and Astrid Oeyre Slind and Swedes Andersson, Sundling and Frida Karlsson pulled clear of the rest of the 60-strong field.
Sundling and Karlsson were struggling to maintain the pace set by the three Norwegians working as a team, but disaster struck in front of the home crowd in the changeover at the halfway mark.
Entering her gate, Slind accidentally kicked away one of her skate skis, greatly slowing her transition and all but ending the medal hopes of the 2023 bronze medalist.
That left Johaug out in front on lap four, but kept at close quarters by Andersson, Weng and Sundling.
That was how things stayed for the next two laps, until coming into the stadium to complete lap five, when a slip on a turn from Sundling saw her fall and take Weng down with her.
The final circuit was a duel for the gold medal, between the current Olympic Johaug and current world champion Andersson.
The 36-year-old Norwegian was doing well to hold off her younger challenger and led the race as they entered the stadium for the final time.
27-year-old Andersson gambled on the final turn, taking a wider line and trying to out-sprint Johaug to the finish line.
The gamble paid off, with Andersson taking victory on the final stretch for the line, despite both competitors receiving a time of 47.51.1.
It is a fourth gold medal at the world championships for Andersson, and her ninth medal in total.
Sundling secured her second medal of Trondheim 2025 by holding off the late challenge of Karlsson, meaning Sweden took three of the first four places.
Weng finished fifth, 11 seconds behind the winner and over a minute clear of Switzerland's Nadja Kaelin, who was sixth.