The Belgians had been outplayed by the Croatian champions for most of the Group A game at the Maksimir Stadium but the tide turned with the arrival of torrential rain midway through the second half.
Bernardez opened the scoring after 74 minutes when his scuffed long-range free-kick crept in and Legear wrapped up the win by finishing off an 89th-minute breakaway.
It was a result that seemed unlikely as Dinamo dominated the first half and spurned a glut of chances.
Croatia international forward Mario Mandzukic was their chief culprit, spurning a number of good openings including one gilt-edged opportunity from 10 yards.
Mandzukic was a constant handful for the visiting defence and almost got his first chance from a low Oeliton Etto cross but the visitors managed to scramble clear.
Mandzukic then hooked a volley wide and also fired a long-range free-kick into the wall.
He then went to ground in the penalty area as he tried to beat Ondrej Mazuch on the left but referee Zsolt Szabo was not interested.
His best opportunity came just after the half hour when the ball was cut back to him unmarked in front of goal but he somehow sidefooted over.
Mandzukic then went down again on the edge of the area under a challenge from Olivier Deschacht but nothing was given.
Anderlecht were limited to just one decent opportunity in the opening period when Cheikhou Kouyate found space on the right but his shot across goal went over.
The visitors began to gain a foothold as they enjoyed more possession early in the second half and Kouyate headed wide.
Dinamo had another good chance after the hour when three players were left free in the box but Ivica Vrdoljak put his header wide.
The hosts were then punished when Bernardez's 35-yard free-kick bobbled under a jumping wall into the bottom corner.
Ilija Sivonjic fired wide as Dinamo responded but Deschacht broke up another attack and set Kouyate clear to create the decisive second for Legear.