With the accompaniment of cannon salutes and the wail of bagpipes, the Union Jack was lowered for the last time on the American mainland at midnight last night, as the former colony of Belize became independent.

The dramatic ceremony marked the end of an empire that once stretched from Canada and the United States to the Falkland Islands off the coast of Argentina.

Only a few specks of territory now remain under British rule in the Western Hemisphere, including the British Virgin Islands, the Falkland Islands, Bermuda, Cayman Islands, Turks and Caicos Islands and Montserrat.

After the Union Jack was lowered, Belize's red, white and blue flag was hoisted over this land of jungles, swamps and islands, once settled by Scottish pirates and now a mixture of 145,000 West Indian blacks, Mayan Indians and whites.

At Belmopan, the small inland capital built a decade ago, Queen Elizabeth's cousin, Prince Michael of Kent, at noon handed over the independence document before representatives of the United States and Cuba, much of the Caribbean and countries as far away as Vietnam and Israel.

The United States and Nicaragua, which are both courting Belize, have sent the largest delegations. The delegation of about 20 from Washington includes Thomas O. Enders, assistant secretary of state for inter-American affairs, and Lt. Gen. William Masterson, deputy commander-in-chief of the U.S. Southern Command in Panama. Managua sent junta member Sergio Ramirez, two Cabinet ministers and a large group of musicians.

Belize, formerly called British Honduras, will now be a parliamentary democracy--its house has 18 seats--and like many Commonwealth nations, it will have Queen Elizabeth as its head of state.

As the present governor leaves for London he is replaced by a Belizean woman, Minita Gordon, as the monarch's representative. George Price, who has been premier since Belize became self-governing in 1964, is now the first prime minister and foreign minister.

Yet despite the flowing oratory here about the "struggle for independence" and a polite toast to the "Queen of Belize" in Buckingham Palace 5,000 miles away, London has been trying to rid itself of this stepchild among its more glorious former colonies for the past 17 years.

The obstacle has been an imperial hangover, a legal tangle with Belize's neighbor that is now more than a century old. Next-door Guatemala claims it inherited Belize from Spain and successive military governments in Guatemala City have threatened to invade when Britain withdraws. The Belizeans, who are used to an open, peaceful society, dread and dislike their more solemn, militaristic and violent neighbors. So rather than throw Belize "to the Guatemalan wolf" as a British diplomat put it, London in recent years has sent frigates, Harrier jump jets and Puma helicopters in defense.

Although pressure from Washington over the past year appears to have tempered Guatemalan nationalist passion, 1,600 British troops costing close to $40 million a year will remain here "for an appropriate period," possibly until a final treaty with Guatemala is signed. A basic framework for agreement already exists but talks broke down in July when Guatemala demanded the right to establish a military base on two Belizean islands off its coast.

It has not repudiated the framework, agreed upon in March, but faced with Belizean independence, Guatemala broke the remaining consular relations with London two weeks ago and closed its borders with Belize.

The centrist Price government has entertained occasional contact with Cuba but to avoid arousing the large conservative contingent at home as well as an apprehensive Washington, Belize and Cuba agreed that Havana would send only a small, low-level delegation to the festivities. Government officials here said that for the same reason there will be no diplomatic relations between Cuba and Belize "in the short term."

As Belize cuts its 300-year-old ties with London it loses close to $6 million a year in administrative help and development aid. But London has offered a parting golden handshake of close to $20 million to ensure continuity of aid for the next few years.

By that time, the Price government trusts it will draw benefits from membership in the United Nations, the International Monetary Fund, the movement of nonaligned nations and other organizations it intends to join.

Richard Barnabey, U.S. consul-general here who has been named charge d'affaires of the new embassy, said the U.S. anticipates increasing its role here. At the moment the U.S. spends between $2 million and 3 million per year on small aid projects and 58 Peace Corps volunteers.