The purpose of mocking in unit testing is to simulate external dependencies or complex behavior that is not directly relevant to the test. By replacing real dependencies with mock objects or functions, you can isolate the unit under test and focus on verifying its behavior independently.