The flag was flown from one of Nelson's warships, HMS Spartiate
The only surviving union jack from the Battle of Trafalgar has sold for more than 20 times its predicted price.
The flag, which was flown from one of Nelson's warships during the battle off the Spanish coast in 1805, was only expected to fetch up to £15,000.
But it sold at auction for £384,000, including a buyer's premium of 20%, at the Trafalgar Day Sale in London.
The flag was presented by the crew of HMS Spartiate to Fife-born Lieutenant James Clephan after the conflict.
Charles Miller, of auction house Charles Miller Ltd, said he was "lost for words" by the amount the flag went for.
He said: "I was absolutely delighted with the result. This demonstrates that this is a unique and charismatic artefact linked to the greatest naval battle of all time and Nelson's greatest triumph which took place 204 years ago today."
The lot was bought by a bidder on a telephone at the auction.
Lt Clephan, who later went on to command his own ship, was one of the few men to have risen through the ranks and was highly admired by his crew.
The presentation of the flag by the 540-strong crew was considered to be one of the highest honours to be bestowed upon an officer by his men.
The flag, measuring 7ft 4in by 11ft 7in, is made of 31 panels sewn together by the crew on board the ship.
It also bears a number of "battle scars" - holes caused by shot and shell splinter damage sustained during the conflict.
The union jack was put up for sale by one of the sailor's descendants, living in Australia.
It was treasured by his family, who kept it in a drawer to preserve it.
Mr Clephan, from Scoonie near Leven in Fife, spent his early years as an apprentice weaver and went on to join the Merchant Navy.
He was forced to join the Royal Navy in July 1794 at the age of 26 and rose to the ranks of midshipman and lieutenant.
At the Battle of Trafalgar, his courage was recognised and he was promoted to first lieutenant immediately afterwards.
In 1811, Mr Clephan was promoted to commander.
He retired in 1840 with the rank of captain and lived in Edinburgh for 11 years until his death at the age of 83.