Shortly after the start of the second half, it looked as if the Scandinavians' defence of the title they won in Beijing four years ago was fizzling out as they trailed by five goals.
But a fine comeback, inspired by two separate four-goal salvos, won them the match and a place in the last four.
Norway had been below their best in the preliminary stages and finished fourth in Group A, setting up an unfavourable draw with the Pan-American champions.
They brought that lacklustre form into this match, with the South Americans opening up a 6-3 lead after just eight minutes.
A continued strong performance from Brazil allowed them to make it 13-9 by the half-time whistle.
After they had extended their lead to 15-9 early in the second half, Norway's defence of their title was on the rocks and they had to deliver a superb performance to avoid an early exit.
Thorir Hergeirsson was not disappointed as his side produced a golden spell to score 10 of the next 13 goals to haul back their deficit and then take the lead at 19-18 for the first time since the third minute.
There was no stopping them and two further unanswered efforts gave them a two-goal cushion with four minutes remaining and that is how it ended, with the ecstatic Norway players leaving the court knowing they had a lucky escape.