Following the course will usually not be a problem, since lectures are generally public. Exceptions are, e.g., seminars with a limited number of places. Also, if the lecture hall happens to be too small (highly unlikely) for all the students, you may be expected to leave.
Check the examination regulations of the PhD program that you would enroll in. Sometimes acceptance for PhD studies can be conditional on taking a few extra courses, depending on what the "Promotionsausschuss" decides. If that can be the case, taking the exam should be no problem, as there needs to be some way to taking the exam in this case.
Getting some kind of certification that you took and passed the exam may be tricky, though. You may have to ask the respective lecturer for a letter stating that, as there are normally no official transcripts for PhD studies.
As already written in a comment, you should check with your advisor if she/he would not oppose taking courses, though - at least if you are not on a scholarship. PhD studies typically works by getting employee status, and using normal employee working hours for such courses may not be welcome. But even on a scholarship, some advisors/supervisors may not like that.