The expression dates back at least to the American Civil War, and may refer to the one-time commander of the U.S. Army, General Winfield Scott. In a May 1861 edition of the New York Times was the sentence:

These gathering hosts of loyal freemen, under the command of the great SCOTT.

In an 1871 issue of Galaxy magazine, there is:

‘Great—Scott!’ he gasped in his stupefaction, using the name of the then commander-in-chief for an oath, as officers sometimes did in those days.

The phrase also appears in the 3 May 1864 diary entry by Private Robert Knox Sneden (later published as Eye of the Storm: a Civil War Odyssey):

‘Great Scott,’ who would have thought that this would be the destiny of the Union Volunteer in 1861–2 while marching down Broadway to the tune of ‘John Brown’s Body’.[1]

Another possible origin of the phrase is that people seeking to emulate the German Prince Albert of Saxe-Coburg and Gotha altered and anglicized “Grüß Gott!”, or “God bless!” into “Great Scott!”. The etymologist and author John Ciardi once believed this, but later recanted in a radio broadcast in 1985. Despite that recantation, the expression is likely to be a minced oath: a mild substitute for invoking the name of God; very possibly derived from the phrase “[by the] grace of God”.

— Wikipedia