For simplicity, let us assume that $K=\mathbb{C}$ (though, these results apply to any field of characteristic zero). We will use capital letters like $X,Y,Z$ to denote matrices while lower case letters like $x,y,z$ shall denote variables.
We will use the fact that the trace of matrices produces an inner product on $M_{n}(\mathbb{C})$ defined by $\langle A,B\rangle=\operatorname{Tr}(AB^{*})$.
If $F$ is a field, then let $F\langle x_{1},\dots,x_{n}\rangle$ denote the ring of non-commutative polynomials over $F$ in the variables $x_{1},\dots,x_{n}$.
We will now go over a few lemmas.
Lemma: Suppose that $f,g\in F\langle x_{1},\dots,x_{n}\rangle$. Then $f=g$ if and only if whenever $r\geq 1$ and
$X_{1},\dots,X_{n}\in M_{r}(F)$, we have $f(X_{1},\dots,X_{n})=g(X_{1},\dots,X_{n})$.
Proof: Suppose that $f\neq g$, and $u>\text{Deg}(f)+\text{Deg}(g)$. Let $V$ be a finite dimensional vector space over $F$ with linearly independent set
$$(e_{i_{1},\dots,i_{k}}|0\leq k\leq u,i_{1},\dots,i_{k}\in\{1,\dots,n\})$$
You can let $X_{1},\dots,X_{n}:V\rightarrow V$ be linear maps such that
$X_{i}(e_{i_{1},\dots,i_{k}})=e_{i,i_{1},\dots,i_{k}}$ whenever $k<u$ and
$X_{i}(e_{i_{1},\dots,i_{k}})=0$ whenever $k=u$. Then $f(X_{1},\dots,X_{n})\neq g(X_{1},\dots,X_{n})$. Q.E.D.
Lemma: Suppose that $f,g_{1},\dots,g_{n}\in\mathbb{C}\langle x_{1},\dots,x_{n},y_{1},\dots,y_{n}\rangle$ and that
$f(x_{1},\dots,x_{n},y_{1},\dots,y_{n})=g_{1}(x_{1},\dots,x_{n})y_{1}+\dots
+g_{n}(x_{1},\dots,x_{n})y_{n}$. Then the following are equivalent.
$f=0$.
$g_{1}=0,\dots,g_{n}=0$.
$\operatorname{Tr}(f(X_{1},\dots,X_{n},Y_{1},\dots,Y_{n}))$ whenever $r\geq 1$ and $X_{1},\dots,X_{n},Y_{1},\dots,Y_{n}\in M_{r}(\mathbb{C})$.
Proof: The directions $1\leftrightarrow 2,1\rightarrow 3$ are clear.
$3\rightarrow 2$ Suppose that 3 holds. We have
$$0=\operatorname{Tr}(f(X_{1},\dots,X_{n},Y_{1}^{*},\dots,Y_{n}^{*}))$$
$$=\operatorname{Tr}(g_{1}(X_{1},\dots,X_{n})Y_{1}^{*}+\dots +g_{n}(X_{1},\dots,X_{n})Y_{n}^{*})$$
$$=\langle g_{1}(X_{1},\dots,X_{n}),Y_{1}\rangle+\dots+\langle
g_{n}(X_{1},\dots,X_{n}),Y_{n}\rangle$$
for each choice of $X_{1},\dots,X_{n},Y_{1},\dots,Y_{n}$. Using basic facts about inner product spaces, we can conclude that $$g_{1}(X_{1},\dots,X_{n})=\dots=g_{n}(X_{1},\dots,X_{n})=0$$ for each choice of matrices $X_{1},\dots,X_{n}$. Therefore, by using the above lemma, we can conclude that $g_{1}=0,\dots,g_{n}=0$. Q.E.D.
For all $k$, let $\phi_{k}:\mathbb{C}\langle x_{1},\dots,x_{n}\rangle\rightarrow \mathbb{C}\langle x_{1},\dots,x_{n}\rangle$ be the $\mathbb{C}$-linear mapping such that $$\phi_{k}(x_{a_{1}}\dots x_{a_{v}})=\sum(x_{a_{i+1}}\dots x_{a_{v}}x_{a_{1}}\dots x_{a_{i-1}}\mid 1\leq i\leq v,a_{i}=k)$$ and $\phi_{k}(c)=0$ for each constant term $c$.
For example, $\phi_{3}(x_{2}x_{3}x_{3})=x_{2}x_{3}+x_{3}x_{2}$. One should intuitively think of the functions $\phi_{k}$ as a formal derivative of the trace operator.
Lemma: (product rule for matrix multiplication) Suppose that $\{E,F\}\subseteq\{\mathbb{R},\mathbb{C}\}$ and $A_{1},\dots,A_{n}:E\rightarrow M_{n}(F)$ are differentiable. Then
$\frac{d}{dt}(A_{1}(t)\dots A_{n}(t))=\sum_{k=1}^{n}A_{1}(t)\dots A_{k-1}(t)A_{k}'(t)A_{k+1}(t)\dots A_{n}(t)$.
In particular, from the cyclicity of the trace, we have
$$\operatorname{Tr}(\frac{d}{dt}(A_{1}(t)\dots A_{n}(t)))
=\frac{d}{dt}\operatorname{Tr}(A_{1}(t)\dots A_{n}(t))$$
$$=\sum_{k=1}^{n}\operatorname{Tr}[A_{k}'(t)A_{k+1}(t)\dots A_{n}(t)A_{1}(t)\dots A_{k-1}(t)]$$
$$=\sum_{k=1}^{n}\operatorname{Tr}[A_{k+1}(t)\dots A_{n}(t)A_{1}(t)\dots A_{k-1}(t)A_{k}'(t)].$$
Corollary: For each $f\in\mathbb{C}\langle x_{1},\dots,x_{n}\rangle$, we have
$$\operatorname{Tr}(\frac{d}{dt}f(A_{1}(t),\dots,A_{n}(t)))=
\operatorname{Tr}[\sum_{k=1}^{n}\phi_{k}(f)(A_{1}(t),\dots,A_{n}(t))A_{k}'(t)].$$
Theorem: Suppose that $f\in\mathbb{C}\langle x_{1},\dots,x_{n}\rangle$. Then
the following are equivalent
$\operatorname{Tr}(f(X_{1},\dots,X_{n}))=0$ whenever $X_{1},\dots,X_{n}$ are matrices.
$f(0,\dots,0)=0$ and $\phi_{k}(f)=0$ whenever $1\leq k\leq n$.
$f\in I$ where $I$ is the sub-vector space of $\mathbb{C}\langle x_{1},\dots,x_{n}\rangle$ generated by the vectors of the form $x_{a_{1}}\dots x_{a_{v}}-x_{a_{2}}\dots x_{a_{v}}x_{a_{1}}$.
Proof: The direction $3\rightarrow 1$ is clear from the cyclicity of the trace. The direction $3\rightarrow 2$ is also clear.
$1\rightarrow 2$. Suppose that $\operatorname{Tr}(f(X_{1},\dots,X_{n}))=0$ whenever $r\geq 0$ and $X_{1},\dots,X_{n}\in M_{n}(K)$. Then
$$0=\frac{d}{dt}\operatorname{Tr}(f(X_{1}+tY_{1},\dots,X_{n}+tY_{n}))|_{t=0}$$ for each choice of $X_{1},\dots,X_{n},Y_{1},\dots,Y_{n}$. However,
$$0=\frac{d}{dt}\operatorname{Tr}(f(X_{1}+tY_{1},\dots,X_{n}+tY_{n}))|_{t=0}$$
$$=\operatorname{Tr}(\phi_{1}(f)(X_{1},\dots,X_{n})Y_{1}+\dots+\phi_{n}(f)(X_{1},\dots,X_{n})Y_{n}).$$
Therefore, we conclude using the above lemma that $\phi_{1}(f)=\dots=\phi_{n}(f)=0$.
$2\rightarrow 3$. Let $$\theta_{i}:\mathbb{C}\langle x_{1},\dots,x_{n}\rangle\rightarrow\mathbb{C}\langle x_{1},\dots,x_{n}\rangle$$ be the linear mapping such that $$\theta_{i}(x_{a_{1}}\dots x_{a_{r}})=\frac{1}{r+1}x_{i}x_{a_{1}}\dots x_{a_{r}}$$ whenever $r\geq 0,\{a_{1},\dots,a_{r}\}\subseteq\{1,\dots,n\}$.
Define a linear operator $L:\mathbb{C}\langle x_{1},\dots,x_{n}\rangle\rightarrow
\mathbb{C}\langle x_{1},\dots,x_{n}\rangle$ by letting
$$L(f)=f(0,\dots,0)+\theta_{1}(\phi_{1}(f))+\dots+\theta_{n}(\phi_{n}(f))-f.$$ Then we have $L(x_{a_{1}}\dots x_{a_{v}})\in I$ whenever $v\geq 0$ and $L(c)=0\in F$, so by linearity, we conclude that $L(f)\in I$ for each
$f\in\mathbb{C}\langle x_{1},\dots,x_{n}\rangle$. Therefore, if $$\phi_{1}(f)=0,\dots,\phi_{n}(f)=0,f(0,\dots,0),$$ then
$f=-L(f)\in I$. Q.E.D.