Shalako's got it. From Wikipedia:
History
In the 16th century, the sergeant major was a general officer. He commanded an army's infantry and ranked about third in the army's command structure; he also acted as a sort of chief of staff to the army's commander.
In the 17th century, sergeant majors appeared in individual regiments. These were field officers, third in command of their regiments (after their colonels and lieutenant colonels), with a role similar to the older, army-level sergeant majors (although obviously on a smaller scale). The older position became known as sergeant major general to distinguish it. Over time, the sergeant was dropped from both titles, giving rise to the modern ranks of major and major general.
The full title of sergeant major fell out of use until the latter part of the 18th century, when it began to be applied to the senior non-commissioned officer of an infantry battalion or cavalry regiment.
It is about this time that the U.S. and British histories of the title diverge, with the American Revolutionary War.
United States
The first official U.S. use of the term was in 1776, when a sergeant major was appointed to the headquarters of each infantry battalion of the Continental Army. In 1920, with the standardisation of the army's enlisted pay grades, it ceased to be a title of rank or grade. However, it survived as the job title of the senior NCO of a battalion, and was re-introduced as a rank in 1958 when Congress authorized the E-8 and E-9 pay grades. The appointment of Sergeant Major of the Army was created in 1966. Command Sergeant Major got separate insignia in 1968.
The U.S. Marine Corps got its first sergeant major in 1801. This was originally a solitary post, similar to the modern Sergeant Major of the Marine Corps, but by 1899 there were five of them. The title was abolished in 1946 but re-introduced as a rank in 1954; the post of Sergeant Major of the Marine Corps was established in 1957.
United Kingdom
The British use of the term was formalised in 1797, when the sergeant major was added to the battalion or regimental staff. When chevrons were introduced as badges of rank, he wore four, later under a crown.
In 1813, cavalry regiments introduced the Troop Sergeant Major to replace the Quartermaster as the senior NCO of a troop; this required the existing position to be explicitly redesignated the Regimental Sergeant Major. Later, the rise of the squadron as the principal sub-regimental unit saw the corresponding introduction of the Squadron Sergeant Major (SSM). The infantry, however, hung on to the undifferentiated, one-per-battalion sergeant major until the eve of the First World War, when the introduction of the Company Sergeant Major forced them to adopt the RSM title as well. (As an infantry regiment could be, and usually was, made up of a number of battalions, one would logically expect the new title to be Battalion Sergeant Major rather than Regimental Sergeant Major: perhaps the infantry felt this would imply a lower status than their cavalry equivalents.)
In 1881, the cavalry RSM and infantry Sergeant Major were among a number of senior non-commissioned positions that were confirmed with warrants, making them warrant officers. This was extended and rationalised in 1915, with the introduction of the new ranks of Warrant Officer Class I (WOI) and Warrant Officer Class II (WOII). RSM became an appointment of the former, CSM and SSM of the latter.