Television cameras filmed Lofven handing his resignation letter to the speaker of parliament, Andreas Norlen.
Lofven, who has been in power for seven years, stepped down as leader of the Social Democrats at the party's congress last week.
Andersson, who is currently finance minister, was elected to replace Lofven as party leader, putting her on track to become the country's first woman prime minister if she wins a vote in parliament.
No date has been set for that vote yet, but it is not expected until next week.
In Sweden's parliament, political forces are so finely balanced that the Social Democrats need the support of both their Green Party coalition partners and the Left and Centre parties to elect a new prime minister.
The Centre Party on Wednesday said it would back Andersson, and the Left is expected to do the same.
The act of installing a woman as prime minister sounds almost anachronistic in a country that has long championed gender equality.
All other Nordic countries -- Norway, Denmark, Finland and Iceland -- have all seen women lead their governments.
The change at the head of the Social Democrats comes as the party hovers close to its lowest-ever approval ratings with elections less than a year away, in September 2022.
The right-wing opposition, led by the conservative Moderates, has in recent years inched closer to the anti-immigration Sweden Democrats and hopes to govern with its informal backing.
More about: Sweden