The formalistic legal theory is that the King/Queen of England (as Duke of Normandy, 133 years before actually gaining that title) gained ownership and control of the Channel Islands, but chose to administer these islands directly, rather than as a political subdivision of England or the U.K.
But, since the military and foreign service of the U.K. had an obligation to serve the needs of the King/Queen of England (and all sorts of other places personally united in the same person), the King/Queen of England gave it that responsibility which economies of scale made inefficient to have the Channel Islands provide for themselves. This arrangement was then continued in 1926 when the personal unification under the same monarch of the various constitutional monarchies and dependencies into a single regime called the British Empire, was superseded by a conception that there were distinct countries, each of which happened to have the same monarch, which came to be known as the British Commonwealth.
How did this arrangement come about?
In 933, the islands were granted to William I Longsword by Raoul King
of Western Francia and annexed to the Duchy of Normandy. In 1066,
William II of Normandy invaded and conquered England, becoming William
I of England, also known as William the Conqueror. In the period
1204–1214, King John lost the Angevin lands in northern France,
including mainland Normandy, to King Philip II of France, but managed
to retain control of the Channel Islands. In 1259, his successor,
Henry III of England, by the Treaty of Paris, officially surrendered
his claim and title to the Duchy of Normandy, while the King of France
gave up claim to the Channel Islands, which was based upon his
position as feudal overlord of the Duke of Normandy. Since then, the
Channel Islands have been governed as possessions of the Crown and
were never absorbed into the Kingdom of England and its successor
kingdoms of Great Britain and the United Kingdom.
The distinction between a military and diplomatic corps that owed a duty to the King/Queen of England as an individual in whatever capacity the monarch saw fit, and a military and diplomatic corps that was a bureaucratic arm of a particular national government among many for which the King/Queen of England was the monarch, really doesn't come into being as a practical matter, until the early 19th century (quoted below) when the executive branches of the British Empire came to be governed more or less exclusively by the prime minsters of the respective national governments (a.k.a. "Dominions") or Crown dependancies, rather than being personally operated non-symbolically by the monarch or by a Governor-General or Bailiff appointed by and serving at the pleasure of the personal and individual will of the monarch.
But, this transition cannot fairly be described as an all or nothing affair that happened at a single moment with a single legislative enactment.
From 1811 to 1820, George III suffered a severe bout of what is now
believed to be porphyria, an illness rendering him incapable of
ruling. His son, the future George IV, ruled in his stead as Prince
Regent. During the Regency and his own reign, the power of the
monarchy declined, and by the time of his successor, William IV, the
monarch was no longer able to effectively interfere with parliamentary
power. In 1834, William dismissed the Whig Prime Minister, William
Lamb, 2nd Viscount Melbourne, and appointed a Tory, Sir Robert Peel.
In the ensuing elections, however, Peel lost. The king had no choice
but to recall Lord Melbourne. During William IV's reign, the Reform
Act 1832, which reformed parliamentary representation, was passed.
Together with others passed later in the century, the Act led to an
expansion of the electoral franchise and the rise of the House of
Commons as the most important branch of Parliament.
The final transition to a constitutional monarchy was made during the
long reign of William IV's successor, Victoria [whose reign ended in 1901].
By the time that the transition of the military and foreign service from a personal set of subordinates of a monarch to divisions of national governmental organizations took place, from about 1811 until 1901, the Channel Islands had shared its military and foreign affairs bureaucracies with the U.K. for more than eight hundred years in the prior arrangement when these bureaucracies were unified under the person of the monarch.
As the military and foreign service increasingly came to be seen as reporting to the U.K.'s prime minister rather than truly reporting to the monarch, the dissonance between the military and foreign service being U.K. agencies and these bureaucracies serving the Channel Islands emerged as something that a logical mind would be troubled by, and so the need to formalize this legally to expressly allow this arrangement which was not logically obvious in the new order arose.
One key moment that solidified this development which had been coming into being piecemeal for about a century at the time, and was largely a fait accompli by the time that it was formally recognized, was the Balfour Declaration of 1926 which ended the notion of all nations with the same monarch being at the highest conceptual level of abstraction, the same country:
Prior to 1926, the British Crown reigned over the British Empire
collectively; the Dominions and Crown Colonies were subordinate to the
United Kingdom. The Balfour Declaration of 1926 gave complete
self-government to the Dominions, effectively creating a system
whereby a single monarch operated independently in each separate
Dominion. The concept was solidified by the Statute of Westminster
1931, which has been likened to "a treaty among the Commonwealth
The countries that shared the King/Queen of England formally known as the British Empire (pre-1926), at this time came to be known as members of the British Commonwealth.
So, formal legislative recognition of the longstanding practice of having the U.K. military and foreign service take care of the affairs of the Channel Islands, an arrangement which abrogated the general rule that each nation or dependencies provided its own governmental services, was probably only enacted in connection with this reconceptualization of the British Empire from 1926-1931.
Any subsequent legislation or regulations or practices merely codified the pre-existing practice. But I am not enough of a technical expert to point to specific enactments that codified this practice.
From 1940-1945, the Channel Islands were occupied by the German Army, after the U.K. evacuated many people because it was incapable of military defending them in 1940. Military defense of the Channel Islands was part of the larger Allied war effort in World War II and there were no foreign affairs to be conducted when the islands were under harsh German military rule.
The legal and political details are described in detail here. The legal basis of this arrangement is currently spelled out piecemeal in a variety of different pieces of local and U.K. legislation. A couple of the more important enactments are that:
Under the UK Interpretation Act 1978, the Channel Islands are deemed
to be part of the British Islands, not to be confused with the
British Isles. For the purposes of the British Nationality Act 1981,
the "British Islands" include the United Kingdom (Great Britain and
Northern Ireland), the Channel Islands and the Isle of Man, taken
together, unless the context otherwise requires.
Incidentally, this arrangement is not unique to the Channel Islands. It is the arrangement that is in place for essentially all self-governing colonial jurisdictions of both the U.K., and of almost all other Western countries. This is what France has done, for example. It is also what is done by the U.S. in the cases of Puerto Rico, the U.S. Virgin Islands, Guam, and the U.S. Marinara Islands (and was previously the case in the Philippines when that was a U.S. dependency).
Indeed, in the case of a territory having democratic domestic self-government, having one's military and foreign affairs handled by another nation in which one does not have a political say is a core part of the concept of a territory as a political dependency of the country in charge of its military and foreign affairs.