They wore the uniform of the country they were visiting partly as a mark of respect, but also because they were, invariably, honourary colonel in chief and/or admiral.
In the case of Nicholas II, his wedding present from his grandmother-in-law, Queen Victoria, was the position of Honourary Colonel in Chief of the Royal Scots Greys. The Royal Scots Greys presented the new Tsar with the entire uniform - down the minutest detail. This is in displayed in the exposition in the Alexander Palace.
Later, Nicholas was created Honourary Admiral in Chief of the British Navy by King Edward VII. This uniform is also exhibited in the Alexander Palace.
When Edward VII and Queen Alexandra visited Nicholas and Alexandra at Reval, the King wore the uniform of an Admiral of the Russian Navy while the Tsar wore his uniform of Admiral of the British Fleet.
tsaria