I don't think that quite works. The problem is, what you know is that $f(x,y) = 0$ for exactly the same values of $x, y$ for which $y - \cos x = 0$. Equivalently, that $f(x, \cos x) = 0$ for all $x$. You don't know that $f(x, y) = y - \cos x$ for general $x, y$. But it is the latter statement that your argument requires.
Still, a variant of your argument works: there are infinitely many $x$ such that $\cos x = 0$. Thus there must be infinitely many solutions to $f(x, 0) = 0$. But the latter is a polynomial, so it must be that $f(x,0) = 0$ for all $x$. Therefore $f(x, y) = yf_1(x,y)$ for some polynomial $f_1$. But then $f(x, \cos x) = (\cos x)f_1(x, \cos x) = 0$ and therefore $f_1(x, \cos x) = 0$ for all $x \ne \pi/2 + n\pi$. By continuity, $f_1(x, \cos x) = 0$ for all $x$. As with $f$, this implies $y \mid f_1(x,y)$ and thus $y^2 \mid f(x,y)$. Continuing, we see that $y^n \mid f(x,y)$ for all $n$, contradicting that $f$ is a polynomial.
Addendum:
A simpler argument: for any $a \in [-1, 1]$, there are infinitely many $x$ such that $\cos x = a$. Therefore $f(x, a) = 0$ has infinitely many roots. Since $f(x, a)$ is a polynomial in $x$, this can only be if $f(x, a) = 0$ for all $x$. Fixing $x, f(x, y) = 0$ for every $y \in [-1, 1]$. Again, since $f$ is a polynomial in $y$, this requires that $f(x,y) = 0$ for all $y$. Therefore $f = 0$ everywhere, not just when $y = \cos x$.