If I make this function without payable and don't take any money from the caller then return value accurately returns me the code.
You are on the right track but misinterpreting what you see. I'll break it down.
accurately returns me the code
I think you mean returns(string memory).
Payable has nothing to do with it. You have to ...
Just in case someone comes here later, in the recent version of ethers, HDNODE has been moved to the utils method. So, in modification to @George's answer above;
const ethers = require('ethers');
const mnemonic = await ethers.utils.HDNode.entropyToMnemonic(ethers.utils.randomBytes(16));
const wallet = ethers.Wallet.fromMnemonic(mnemonic);