Video report by ITV News Royal Editor Chris Ship
The royal is touring The Silverstone Experience in Northamptonshire, a museum charting British motor racing through the years, alongside Hamilton.
Harry was driven to the doors of the new building in an electric Mercedes and was greeted by dignitaries including former world champion F1 driver Damon Hill - who took the crown with the Williams team in 1996.
The Duke also met some of the current crop of drivers, Williams' George Russell and Red Bull racer Alex Albon.
The experience is the new home of the British Racing Drivers’ Club’s (BRDC) archive and also boasts hands-on exhibits for visitors.
Harry managed to beat Lewis on the timed challenge, causing both men to laugh, with the duke saying: "They've stitched you up."
The world champion, perhaps showing he was better off behind the steering wheel, said: "Bloody hell, 10.7 seconds? Race lost!"
Pointing at Harry he then asked museum staff: "How much did he pay you for that?"
Officially opening the building, Harry said: "I'll keep this brief - there's nothing better than officially opening a building that is very much open."
Harry said: "[Saturday] marks two years since we were last here and I can't believe that you've manged to turn a World War Two hangar that was pretty cold, pretty dusty, two years ago, into this remarkable experience.
"Thank you for being able to do that under a huge amount of pressure as well, thank you for providing so many of these opportunities for so many young people.
"And thank you for really putting a marker down for what British motor racing means to the world, touching on what Lewis said."
He added: "It's a long time coming, having walked through it now I am probably going to have to come back to actually really soak up the experience for myself."
The duke and Hamilton met students from local schools in the Tech Lab section, learning more about the cutting-edge science and engineering that goes into motor racing.
The visit marks a final run of royal engagements for Harry since he and his wife Meghan announced they would be stepping down as senior royals.
The couple are making their last official appearances as HRHs alongside the rest of the royal family at the Commonwealth Day service in Westminster Abbey on March 9.
Hamilton, who drives for Mercedes, begins his bid to take a record-equaling seventh world title when the new Formula One season kicks off with the Australian Grand Prix next weekend.