Play video content EXCLUSIVE 6/24/19 TMZ.com

Louis C.K.'s surprise appearance at a comedy festival sent the crowd into a frenzy ... roaring in approval as he took the stage.

The comedian hit up Skankfest NYC Saturday night in Brooklyn ... where the crowd had ZERO clue he was about to take the mic. Check out the video ... the packed house gave Louis a standing ovation. Some of them literally jumped for joy. You can see a woman going berserk.

We're told Louis opened up his set by quickly addressing the controversy surrounding him ... telling the crowd, "If you ever masturbate in front of someone, ask them first. If they say yes, don't do it!"

Louis had the crowd laughing their asses off for about 20 minutes before calling it a night. Remember, last time he made a surprise appearance, back in September, he also got quite the ovation.

We're told the festival's promoter, Luis J. Gomez, had been working hard to get Louis C.K. to agree to perform. And, with days to spare before the festival officially kicked off, Gomez locked him down. 'SNL' star Michael Che followed Louis' set. Bill Burr also performed.

Louis, of course, is still in the midst of making a comeback since his 2017 confession -- ﻿admitting he'd exposed himself to several women and masturbated in front of them or over the phone.