Lushomo Mweemba opened the scoring for Zambia inside the first three minutes and Barbra Banda added a second from the spot midway through the first half.
Costa Rica’s Melissa Herrera pulled a goal shortly after the break, but Racheal Kundananji completed the win, netting a third in injury time.
Both sides had already been eliminated after two matches.
Zambia had lost 5-0 against both Japan and Spain, but their campaign at least ends on a high.
They made a brilliant start in Hamilton as Mweemba scored their first World Cup goal, volleying home Avell Chitundu’s corner after two minutes and 11 seconds.
Skipper Banda doubled their advantage from the spot – the 1000th goal in the Women’s World Cup – after her shirt was pulled by Katherine Alvarado in the penalty area.
Costa Rica, who were also chasing a first World Cup win in just their second tournament, rallied and struck the crossbar through Maria Paula Coto just before the break.
After the restart Amelia Valverde’s team halved the deficit with Herrera nodding in Valeria del Campo’s header across goal.
An equaliser seemed inevitable with Zambian keeper Catherine Musonda catching Priscilla Chinchilla in the box, but a lengthy video assistant referee review ruled out a penalty kick due to an offside in the build-up.
Herrera then found the net for a second time midway through the second half, but her strike from a tight angle was ruled out for offside.
With Costa Rica pushing for an equaliser, Banda’s disguised pass sent through substitute Kundananji who sealed a famous win.
Zambia finished third in Group C, ahead of Costa Rica in fourth.