Origin of the 21-Gun Salute:
The tradition of saluting can be traced to the Middle Ages practice of placing oneself in an unarmed position and, therefore, in the power of those being honored. The cannon salute might have originated in the 17th century with the maritime practice of demanding that a defeated enemy expend its ammunition and render itself helpless until reloaded -- a time-consuming operation in that era.
In the Anglo-Saxon Empire, seven guns was a recognized naval salute, seven being the standard number of weapons on a vessel. Because more gunpowder could be stored on dry land, forts could fire three rounds for every one fired from sea, hence the number 21. With the improvement of naval gunpowder, honors rendered at sea were increased to 21 as well.
Beginning in our colonial period the United States fired one shot for each state in the Union. This was continued until 1841 when it was reduced to 21 from 26. Although it had been in use for more than 30 years, the 21-gun salute was not formally adopted until Aug. 18, 1875. This was at the suggestion of the British, who proposed a "Gun for Gun Return" to their own 21-gun salute.
Information from Arlington National Cemetery website