It's not like a novice beating a master. It's more like a talented novice with experience in a related discipline managing to hold out against an overconfident, injured, emotionally unstable student, capitalising on their mistakes, then reacting quickly when their misjudged endgame fails:
- For most of the fight, Rey was on the defensive. The fight reached a point where Kylo could have killed her, but instead chose to try to convert her to the dark side - he'd pretty much won, but made a mistake she reacted quickly to.
- Kylo was injured. He had an absolutely massive Chewie-crossbow wound on his side - and despite what certain films like to lead viewers to believe, injuries impact fighting ability. Really, it's amazing he could fight at all. Also, he was tired and lightly wounded from fighting Finn (related question with a similar answer: Why did Finn and Kylo's fight not end sooner?)
- Rey's tough and experienced in fighting with unfamiliar weapons. Rey may be untrained with this particular weapon, but she's spent her whole life as a scavenger, defending herself using whatever she can get her hands on. It was established early on that she's got very good reactions, is fast, agile, good at improvising, and good at defending herself.
- Kylo was a promising student, not a master. It was explicitly stated by Snoke that he hasn't completed his training. Still, he had much more training than Rey - which is why he had the upper hand for most of the fight, despite his other temporary disadvantages.
- Lightsaber fighting is more about focus, speed, and reactions than physical strength, so sex isn't really a factor. Kylo's focus and reactions were well below usual due to a) the fact it was already well established that his biggest flaw was his inability to control or focus his rage and b) that he was even more emotionally unstable than usual, given recent events.
- Kylo was an unfocused emotional mess, hacking wildly. All those points where Rey temporarily gained an upper hand were by capitalising on Kylo making wild, poorly judged, exploitable mistakes - which makes perfect sense given all the above.
The only thing unanswered here is how both Rey and Finn managed to never accidentally cut themselves, not even once, across three lightsaber fights with no training... I'm not sure there is a good answer to that one, unfortunately.