Stokes said he was subjected to "repeated abuse from the crowd" when he was walking off but "I should not have reacted in that way."
Stokes released a statement to apologize after his swearing was heard on the live television coverage. The England team said members of its support staff were also verbally abused during and after play at the Wanderers in Johannesburg.
Stokes was clearly angered by comments directed at him as he headed for the players' tunnel after being dismissed for just 2. He shouted "come and say it to me outside the ground" before aiming two expletives at a person in the crowd who hasn't been identified. The expletives included a disparaging remark about someone wearing glasses.
Stokes turned to his right and pointed with his right hand in the direction of nearby fans during the verbal altercation. It only lasted a few seconds before he headed up the players' tunnel.
"I wish to apologize for my language that was heard on the live broadcast today after my dismissal," Stokes said. "I should not have reacted in that way. As I was leaving the playing area, I was subjected to repeated abuse from the crowd.
"I admit that my reaction was unprofessional."
The England team said it was "personal abuse" of Stokes by a fan near the tunnel.
"It is disappointing that a member of the public has gone out of their way to abuse Ben as he was leaving the field," England director of cricket Ashley Giles said. "In addition to this incident, members of our support staff were subjected to personal abuse during and after the day's play."
England has asked for "enhanced security" at the stadium for the remainder of the test.
England leads 2-1 and can clinch the series with a draw at the Wanderers, which is renowned in South Africa for its raucous crowds.
The 28-year-old Stokes could still find himself in trouble if match referee Andy Pycroft of Zimbabwe decides to take action.
Stokes, who is England's vice captain, had just won back his reputation by being crowned the world player of the year for 2019 and being awarded an OBE by the Queen. That was in recognition of his stellar 2019, when he was pivotal in helping England win the Cricket World Cup and also played one of the most memorable test innings to make a century and snatch victory from almost-certain defeat in an Ashes test against Australia.
But his apparent challenge to the fan to have a fight outside the stadium drags up a checkered disciplinary past.
Stokes faced criminal trial in Britain over a street brawl outside a nightclub in late 2017. He was acquitted after he said he got involved to defend a gay couple from homophobic abuse. He was fined and banned by the England and Wales Cricket Board for bringing the game into disrepute.
Stokes was also sent home from an England youth team tour early in his career for breaches of discipline.
England had slipped from 107-0 to 157-4 at the Wanderers when Stokes was out and was under pressure. The tourists recovered well to 192-4 by stumps.
Stokes has continued his brilliant form in South Africa to help England turn it around after losing the opening test. He made a game-changing half-century in the second test in Cape Town and also took six catches and the match-winning wickets in that game. He made a century to help England win the third test by an innings and 53 runs to go 2-1 up.
The series has generally been played in a good spirit although there have been two previous flash points.
England wicketkeeper Jos Buttler was fined and received a demerit point for unleashing a series of swear words at South Africa's Vernon Philander while Philander was batting in the second test in Cape Town. Buttler's verbals were also heard on the TV coverage.
South Africa fast bowler Kagiso Rabada is banned for the final test for a provocative celebration in the face of England captain Joe Root after he got Root out in the third test. Rabada was also fined and given one demerit point. The point was his fourth in a two-year period and took him over the threshold and triggered a one-game ban.
(This story has been corrected to show that Stokes made a century to help England win the third test, not second test, by an innings and 53 runs.)