Knowing the formal rules, though processes, steps to take, fundamental principles conciously is not the same as being able to apply those things on autopilot.
The former makes a good teacher, being able to explain exactly why and how, through concious effort, the latter makes a skilled person.
You can have the former without the latter or vice versa. Often people who are very skilled have internalised the rules to such an extend they cannot seperate the rules from the whole and are thus unable to teach. Also often the people with the theoretical knowledge spend most of their time formalising those rules than applying them in day to day work, and as such aren't as skilled.
So no you don't have to be a good mathematician to teach maths, you don't need to be a good programmer to teach coding. Being a good programmer is applying all those rules instinctively, quickly, without effort, you have to feel instinctually that a piece of logic is "not nice". But teaching requires the ability to apply all these steps conciously, out loud, step by step, formally, not quickly. Same for a mathematician, same for an artist, same for any job.
Skill is not neccecarily knowledge and knowledge is not neccecarily skill.