Tesla founder Elon Musk has appeared in court in Los Angeles to answer a lawsuit brought by a British cave diver he called "pedo guy" on Twitter.
Vernon Unsworth, who helped rescue 12 boys trapped in a Thai cave last year, is suing for defamation.
Mr Musk, the first to testify at the court, said Mr Unsworth had insulted him, so he had insulted him back.
The 48-year-old said the "pedo guy" tweet had not been meant to be taken literally.
Mr Unsworth's legal team have described Mr Musk's now-deleted tweet as "vile and false" and are seeking unspecified punitive damages.
The Tesla and SpaceX billionaire posted the message after Mr Unsworth publicly rejected his proposal to use a mini-submarine to rescue the boys, members of a football team who became trapped deep inside a cave in northern Thailand in June 2018 in a case that captured the world's attention.
But in an interview on CNN after the successful rescue, Mr Unsworth called the idea a "PR stunt" and suggested the American "stick his submarine where it hurts". Two days later, Mr Musk wrote a series of tweets including one describing Mr Unsworth as a "pedo guy".
In his court testimony, Mr Musk - who has 29.8 million Twitter followers - said Mr Unsworth's comments were "wrong and insulting, and so I insulted him back", adding: "It was an unprovoked attack on what was a good-natured attempt to help the kids."
He said he had thought Mr Unsworth "was just some random creepy guy" and "unrelated to the rescue", and that he had not expected the tweet to be taken literally. "I assume he didn't mean to sodomise me with a submarine... Just as I didn't literally mean he was a paedophile."
The Tesla boss apologised to the cave diver in court, looking directly at him and saying: "I apologised in a tweet and again in the deposition, and I'll say it again: I apologise to Mr Unsworth." Mr Unsworth did not testify on Tuesday.
Mr Musk's lawyer, Alex Spiro, said in his opening statements that the term "pedo guy" was a common insult in South Africa, where the billionaire grew up, meaning "creepy old man", and described the messages as "joking, taunting tweets in a fight between men".
But Lin Wood, a lawyer for Mr Unsworth, tried to show that Mr Musk had meant what he said by citing a separate tweet in which Mr Musk, after being questioned about the allegation by other users, said, "Bet ya a signed dollar it's true." That tweet was also later deleted.
Then, in an email exchange with a Buzzfeed reporter who had contacted him for comments on a threat of a legal case by Mr Unsworth, Mr Musk said, "Stop defending child rapists."
In the packed courtroom, Mr Musk also acknowledged paying $52,000 (£40,000) to a man who had posed as a private detective to dig up information on Mr Unsworth after it became clear he would be sued. The investigator turned out to be a conman, Mr Musk said.
Mr Musk's comments on Twitter have been controversial on other occasions and in April he reached a settlement with the Securities and Exchange Commission (SEC) over his tweets, which also puts a restriction on his use of the platform.
The judge has denied the defence's request to define Mr Unsworth as a "public figure" - meaning lawyers for Mr Unsworth do not have to prove Mr Musk acted with "actual malice", lowering the bar necessary to win the case.