Britain looked down and out after losing captain Tom Mitchell to injury in the first minute and then falling 21 points behind.
But they summoned remarkable recovery powers to stun their opponents 26-21, scoring four unanswered tries at Tokyo Stadium, and they now face New Zealand in the semi-finals on Wednesday.
Ollie Lindsay-Hague, Ben Harris, Alex Davis and Dan Norton claimed touchdowns, while Dan Bibby kicked three conversions.
It means that the Rio Games silver medallists remain firmly on course for another tilt at gold.
And that is a spectacular achievement in itself, given the prospects for the men's and women's squads looked bleak last year amid coronavirus-enforced cuts made to sevens programmes in Britain.
Funding, though, was then obtained via a commercial partnership struck between the Rugby Football Union, Scottish Rugby Union, Welsh Rugby Union and The National Lottery promotional fund.
Reflecting on the comeback win, Scotland sevens international Robbie Fergusson said: "We spoke a lot as a group about what this means to us, what boys are doing after this, what everyone is about.
"So we know there is the collective will and desire to do well here. When our backs are against the wall, we seem to be at our best."
Bibby added: "We knew that if we kept in it, kept working it, those last couple of minutes would be the most important. I think we showed that.
"We ground it out, stuck at it and the experience of the squad really showed those last few minutes.
"We were 21-0 down and the gold medal was flashing before your eyes in those moments. But it was in those moments where we looked round at each other, and all gave that confident nod."
Earlier in the day, Britain suffered a 33-7 defeat against Olympic champions Fiji in their final Pool B outing.
Both teams had already qualified for the quarter-finals, but Fiji produced their best performance of the tournament to take top spot.
There was agony for Olympic debutants Ireland, though, as they bowed out of the main competition on points difference.
After losing both of their games on Monday, tries by Hugo Lennox and Harry McNulty, plus a Billy Dardis conversion, secured a 12-7 victory over Kenya, but it was not enough to progress.
Fiji dominated a rematch of the 2016 Rio final against Britain as they ran in first-half tries from Asaeli Tuivuaka, Sireli Maqala and Jiuta Wainiqolo.
Although Harris claimed a consolation try that Bibby converted early in the second period, Fiji claimed two further touchdowns and put down a strong gold medal marker.