Justin Trudeau received an honorary degree from the University of Edinburgh, then went on a walkabout in the city centre.
Later, had a private audience with the monarch at the Palace of Holyroodhouse.
He is in Europe ahead of a key G20 summit in Germany, when trade issues are expected to dominate the agenda.
Mr Trudeau met Irish Taoiseach Leo Varadkar and President Michael D Higgins in Dublin on Tuesday.
The visit comes as Canada celebrates the 150th anniversary of its various provinces uniting into one country.
Asked about his visit to Scotland during his walkabout in Edinburgh, he told the BBC: "It's wonderful, thank you. It's great to be back."
He also quipped that he would take credit for the sunny weather the city is enjoying.
Asked if he thought Brexit was a mistake, Mr Trudeau chose not to answer.
The Canadian premier walked from the MacEwan Hall at Edinburgh University to the Museum of Scotland, waving to locals and wellwishers along the way.
The 45 year old was elected as the second-youngest prime minister in Canada's history two years ago.
Ahead of the visit to Scotland, he said he wanted to honour the Queen's "commitment to public service", which he said had "long inspired me and many other Canadians".
And he said he wanted to "recognise the strong family ties, history and common purpose our countries share".
Mr Trudeau previously met the Queen in Buckingham Palace shortly after his election in 2015, having done so several times as a child in the 1970s when his father Pierre was prime minister of Canada.
Mr Trudeau has Scottish ancestry, with his grandfather James Sinclair being born in Banffshire before his family emigrated to Canada in 1911.
The Queen is in Edinburgh for the royal family's traditional Holyrood Week events.
She met First Minister Nicola Sturgeon and Ken Macintosh, the presiding officer of the Scottish Parliament, during private audiences at the palace on Tuesday.
The Canadian prime minister has met the Queen in Edinburgh as he made his first official visit to Scotland.