Trailing 26-0 with just over four minutes of the 14 minute tie remaining, the Scots looked dead and buried in the straight knock-out tournament.
However, Harvey Elms grabbed a try to give a glimmer of hope and John Dalziel’s men never gave up.
A second try came from skipper Scott Riddell - this one converted by Robbie Fergusson - and a penalty try worth seven points then followed.
A Kenyan was yellow carded for stopping Elms scoring when that penalty try was given, but even then still down 26-19 time was running out.
However, somehow Farndale managed to squeeze in at the corner for the Scots’ fourth try and Fergusson kept his nerve to drop kick the conversion from the corner to make it 26-26.
By this point the Kenyans had lost the plot and were reduced to five men with a second yellow card before, in injury time, Farndale went in at the opposite corner for a dramatic winner.
Head coach Dalziel said: “I’m hugely relieved. We were staring down the barrel for a while there, but I feel in times like that you find out what you’ve got as a group.
“We’ve got a young team who have really came together on an off the field and we’ve said all week that there is something brewing with this team. They’ve learned a lot of harsh lessons and have been on the wrong side of things all series it was their moment.
“I thought the impact off the bench was outstanding, we knew that Jack Cuthbert and Max McFarland would have a say in the game coming on at half time but probably not as much as that.
“In the second half, Jack and Jamie Farndale’s work at the restart and the kicking of Robbie Fergusson earned us some key possession.
“We’re very honest and we know how close we came to a disappointing start and to being knocked out of the main competition. We’ll take the lessons from this and move on.”
The Scottish team will now regroup and face South Africa in the quarter-finals on Saturday night 11.32pm UK time. The South Africans thumped Ireland 45-7 in their last 16 tie.
Earlier Kenyans were only in the Scots’ half twice in the first half and came away with 14 points.
A try right from the second half kick-off then put them 19-0 up and when try number four came and was converted they had one foot in the last eight - before the amazing turnaround.
Scotland team v Kenya: Scott Riddell, Nyle Godsmark, Jo Nayacavou, Robbie Fergusson, Gavin Lowe, Harvey Elms, Jamie Farndale. Subs used: Max McFarland, Jack Cuthbert, Alec Coombes. Unused: Craig Jackson, Sam Pecqueur.
Scotland scorers: Tries: Elms, Riddell, penalty try, Farndale 2. Cons: Fergusson 3.