Latvia made it 3-1 at 19:44 when Zile scored on a shot from the blue line through traffic during a delayed penalty.
Tralmaks made it 3-2 at 6:41 of the second period, finding space at the far post to tap in a pass through the crease by Zile from the left boards.
Latvia pushed for the tying goal, but Dans Locmelis hit the crossbar during a power play with 3:32 remaining in the second period, and Tralmaks hit the goal post on the first shift of the third period.
"We dug ourselves a hole and then we didn't capitalize on the chances to tie it up," Latvia forward Teddy Blueger said.
Andersen held strong as Latvia outshot Denmark 14-6 in the third period, and Olesen scored into an empty net at 17:38 of the third period for the 4-2 final.
"He's amazing, one of the best goalies in the world," Olesen said. "He's calm and steady. We're very happy to have him."
NOTES: Olesen is the first Denmark player with a multigoal game at an Olympics that included NHL players. ... Latvia forward Zemgus Girgensons had an assist on Tralmaks' goal. It was his fourth in eight career Olympic games, tying him with Sandis Ozolinsh for most among Latvia players at an Olympics featuring NHL players.