Tech entrepreneur Elon Musk has told his defamation trial he sent an inflammatory Twitter message in response to an "unprovoked" insult he had received from the plaintiff.

Key points: Elon Musk says Vernon Unsworth's comments were "an unprovoked attack on what was a good-natured attempt to help the kids"

Elon Musk says Vernon Unsworth's comments were "an unprovoked attack on what was a good-natured attempt to help the kids" Lawyer Taylor Wilson says Mr Musk's tweet calling Mr Unsworth "pedo guy" overshadowed the diver's role in the Thai cave rescue

Lawyer Taylor Wilson says Mr Musk's tweet calling Mr Unsworth "pedo guy" overshadowed the diver's role in the Thai cave rescue Mr Musk's attorney, Alex Spiro, says the lawsuit is over "joking, taunting tweets" in a fight between men

Mr Musk, the billionaire chief executive of electric car company Tesla, was the first witness to testify in Los Angeles on Tuesday (local time), in the lawsuit brought by a British cave diver who gained fame in the rescue of 12 boys and their soccer coach from a flooded cave in Thailand last year.

Vernon Unsworth said Mr Musk, who also founded the rocket company SpaceX, falsely labelled him a paedophile on Twitter and should pay punitive and other damages for harming his reputation.

The case stems from an offer Mr Musk made to furnish a mini-submarine from SpaceX to assist in the July 2018 cave rescue.

Mr Unsworth told CNN on July 13, 2018, three days after the rescue was completed, that Mr Musk's offer was a "PR stunt" and that Mr Musk should "stick his submarine where it hurts".

Two days later, Mr Musk lashed out at Mr Unsworth in a series of tweets, including one calling the cave diver a "pedo guy".

Mr Musk later apologised for the comment, which Mr Unsworth called a lie.

Mr Musk was called after a jury was selected to hear the case and the two sides delivered opening statements.

British cave expert and diver Vernon Unsworth, left, arrives for the trial against Mr Musk at the US District Court in Los Angeles. ( AP: Mark J. Terrill )

Mr Musk said he was merely responding in kind to Mr Unsworth's remarks.

Those comments were "an unprovoked attack on what was a good-natured attempt to help the kids," Mr Musk testified.

"It was wrong and insulting, and so I insulted him back.

"I thought he [Mr Unsworth] was just some random creepy guy."

"I thought at the time that he was unrelated to the rescue."

Pressed under questioning by lawyer Lin Wood, Mr Musk testified that he did not mean for his tweet about Mr Unsworth to be taken literally.

"I assume he did not mean to sodomise me with a submarine … just as I didn't literally mean he was a paedophile," he said.

'Ruined one of the man's proudest moments'

Mr Unsworth's lawyer Taylor Wilson said the tweets ruined what should have been one of the diver's proudest moments" and overshadowed his role in rescuing the boys and their coach.

Mr Unsworth said Mr Musk harmed his reputation. ( AP: Sakchai Lalit )

Mr Musk's attorney, Alex Spiro, said the lawsuit was over "joking, taunting tweets" in a fight between men.

Mr Spiro said Mr Unsworth deserves nothing.

The judge said the case hinged on whether a reasonable person would take Mr Musk's Twitter statement to mean that he was calling Mr Unsworth a paedophile.

To win the defamation case, Mr Unsworth needs to show that Mr Musk was negligent in publishing a falsehood that clearly identified the plaintiff and caused him harm.

"Actual malice" on Mr Musk's part does not need to be proven because the judge has deemed Mr Unsworth a private individual rather than a public figure.

A jury of five men and three women will weigh the case, which seeks unspecified damages from Mr Musk.

Although the case does not involve Tesla, Mr Musk's Twitter habits have long been under close scrutiny, with investors and regulators expressing concerns about his tweets.

With 29.8 million followers, Mr Musk's Twitter account is a major source of publicity for his Palo Alto, California-based electric car company, which does not advertise.

Reuters/AP