Stokes said the violence occurred when Ali verbally abused him and threatened to attack him with a bottle.
Giving evidence for a second day at Bristol crown court, Stokes told the jury he had drunk a bottle of beer immediately after England's one-day international win against the West Indies and then had two or three pints during dinner.
He then went into Bristol city centre with several England teammates and had five or six vodka and lemonades and possibly some Jagerbombs, the court was told.
Stokes said he could not remember punching Ali or his friend, Hale, and said he intervened because they had directed homophobic abuse at Barry and O'Connor.
Stokes admitted he could not remember what was said.
Nicholas Corsellis, prosecuting, asked Stokes: “You don’t really remember significant parts of this incident, for example knocking Mr Ali out? Is that because you were really very drunk?”
Rejecting the suggestion, Stokes told the court: “Ali was aggressive and violent towards me in what he said. But he was definitely verbally aggressive with Mr Barry and Mr O’Connor.
“It’s clearly in my statement that I admit to throwing multiple punches. At the time of that situation, I constantly felt under threat from Mr Ali.”
The cricketer told the court he had not mocked nor been homophobic towards Barry and O’Connor outside the Mbargo nightclub.
He said he could not remember flicking his cigarette butt at them nor knocking Ali unconscious a short time later.
Stokes was asked about what Ali was doing in the moments before he knocked him out and he said he could not remember.
Corsellis asked: “Is it because you are hiding behind your lack of recollection because you know full well you carried out a retaliatory attack upon those two men, first Mr Hale and then Mr Ali?”
Stokes replied: “No, all my actions were in self-defence and fearing for my safety.”
Stokes, of Castle Eden, Durham, and Ali, of Bristol, each deny a charge of affray.
The trial continues.