Scotland captain Greig Laidlaw came off the bench to deny Japan a famous victory as the Brave Blossoms fell to a 21-16 defeat on Saturday.
With Emperor Akihito and Empress Michiko in attendance at Ajinomoto Stadium to watch a national team game for the first time, Japan took command midway through the first half when Kaito Shigeno scored a stunning try following a lacerating end-to-end passing move.
But steady kicking by the Scots kept the visitors in the game, and star player Laidlaw landed four out of four penalties after coming on early in the second half to edge his side in front with 10 minutes remaining.
"I'm really proud of the effort," said Japan interim coach Mark Hammett. "I think we all witnessed a really outstanding game here in Tokyo.
"I don't think the best team won and even talking to the Scottish coaches, they agree. But at the end the score is on the board and it will go down as a lost test match."
A 34,073-strong crowd showed up at the stadium that will host the opening game of the 2019 Rugby World Cup hoping that Japan could avenge last week's 26-13 loss to the same opponent at Toyota Stadium.
But instead the Scots maintained their perfect test-match record over the Brave Blossoms, with starting scrum half Henry Pyrgos also kicking three penalties to add to Laidlaw's tally.
"In the context, at the end of the season and a long year, I'm happy to grind out a win," said Scotland coach Vern Cotter.
"I'm happy with the way we applied pressure and got the result because it could have gone the other way. They flew into us and it took us a while to wear them down. We're happy with the result."
Japan center Tim Bennetts thought he had bagged the first try of the evening when he intercepted a wayward Scottish pass and cantered over the line in the ninth minute, only for Italian referee Marius Mitrea to call the play back for a high tackle.
But the home side would only have to wait another 11 minutes before Shigeno crossed the line, putting the finishing touches to a fantastic passing move that took Japan from one end of the pitch to the other.
"The backs were brave and opened up the space to go forward," said Japan captain Shota Horie. "It was a great try."
Pyrgos' kept the visitors within touching distance, however, and Japan headed into halftime with a 13-9 lead.
Yu Tamura then stretched Japan's lead with another penalty 10 minutes into the second half, before Laidlaw emerged from the bench to return fire for the visitors.
Laidlaw reduced the deficit to one point with a monster kick from almost the halfway line, and the scrum half edged the Scots into the lead with another in the 71st minute.
The Gloucester player then sealed the deal with his fourth and final penalty three minutes from time, leaving Japan with nothing to show for their efforts.
"We're looking toward the 2019 World Cup and testing ourselves against the big teams," said Horie. "We need to take responsibility and put in the effort.
"We had some young players out there today and to play such a close game will give them good experience. Not just young players but also the veterans still have a lot of work ahead of us."
With your current subscription plan you can comment on stories. However, before writing your first comment, please create a display name in the Profile section of your subscriber account page.