This feature is native to MacOS, but you need to activate it first.
Enable password protection for the screen saver:
- From the Apple menu > System Preferences > Security and Privacy.
- In the "General" tab: check the "Require password" and "immediately".
- As from now the keyboard short-cut to launch the screen saver (CTRL-SHIFT-EJECT) will lock the Mac: you will need to enter your user password to unlock it.