CANBERRA, Australia, May 21— Sir John Gorton, who became prime minister of Australia by accident and voted himself out of office after three years, died in Sydney on Sunday. He was 90.

Sir John, the first Australian senator to become prime minister, succeeded Harold Holt, who drowned while surfing near Melbourne in December l967.

John Grey Gorton was born in Melbourne in 1911 to the daughter of a railroad worker. His father, a wealthy businessman, was not married to his mother. His career was unconventional from the start, and the most recent biography of him was ''John Gorton: He Did It His Way,'' a title that admirers and critics could consider an apt epitaph.

Sir John's most lasting political achievement was his decision to finance a national film school, a major contribution to developing the now buoyant Australian movie industry.

He was a classmate of Errol Flynn in school and later studied at Oxford University. There he rowed, earned a master's degree and learned to fly, enabling him to enlist in the Royal Australian Air Force as a fighter pilot in l940. He flew missions in Britain, New Guinea and Malaya and was badly wounded and scarred when his Hurricane aircraft crashed near Singapore in l942. A few weeks later he again narrowly escaped death aboard an evacuation freighter sunk by a Japanese torpedo.

After the war, he became a fruit farmer along with his wife, Bettina Brown, an American whom he met in Spain on a college vacation and married in 1935.

He entered rural politics and was elected to the federal Senate in l949, when Australia began a 23-year period of conservative administrations. He held many cabinet posts, but was unprepared for the national leadership, which was thrust upon him in 1968 after Prime Minister Holt died.

He was a member of the Liberal Party, and was indeed liberal in his politics, especially on social issues, unlike the party's conservative core. But mainly he was a nationalist and an individualist, and was often at odds with machine politics.

He continued Australia's military involvement in Vietnam, which had begun in l964, but had disputes with United States officials, whom he pressed for information about their intentions in Indochina.

Prime Minister Gorton's relations with important colleagues were also soured by disagreements, some about his style, others about major issues like states' rights. In l971, his enemies within his party pursued a no-confidence vote against him. With the party caucus deadlocked, he used his own deciding vote against himself, stepping down from office almost as dramatically as he had entered and retiring from politics in l972. He was knighted in 1977.

Sir John, whose first wife died in l983, is survived by his second wife, Nancy Home, whom he married in l993, and two sons, a daughter, 12 grandchildren and four great-grandchildren.

Photo: Sir John Gorton. (Reuters)