As you say, it is not difficult to prove that $g=0$ when there are finitely many fluctuations in sign.
More precisely, suppose we have points $0=a_0,a_1,\dots,a_n=M$ such that $g\geq0$ or $g\leq0$ in each interval $[a_i,a_{i+1}]$. Then, if we have measures $\mu_\theta$ as you say and $\int_{0}^{\theta} g(z) d \mu_{\theta} (z)=0$ for all $\theta$, then $g=0$ almost everywhere.
More in general, if the function $g$ satisfies that for every $x\in[0,M]$ there is some $\varepsilon>0$ such that $g\geq0$ or $g\leq0$ in $[x,x+\varepsilon]$, we can prove in the same way that if $\int_{0}^{\theta} g(z) d \mu_{\theta} (z)=0$ for all $\theta$, then $g=0$ (consider the maximum $x$ such that $g\neq0$ almost everywhere in $[0,x]$, and if $x<M$ obtain a contradiction).
If $g$ is not as in the previous paragraph (this of course implies $g\neq 0$), then we can find some measures $\mu_\theta$ as in the question so that $\int_{0}^{\theta} g(z) d \mu_{\theta} (z)=0$ for all $\theta\in[0,M]$.
To do it, let $k=\inf\{x\in[0,M];\not\exists\varepsilon>0\text{ such that }g\geq0\text{ or }g\leq0\text{ in }[x,x+\varepsilon]\}$. Then we can prove as before that $g=0$ a.e. in $[0,k]$. Moreover, by definition of $k$, for each $n\in\mathbb{N}$ there are sets of positive measure $A_n,B_n$, such that $A_n\subseteq[k,k+\frac{1}{n}],B_n\subseteq[k,k+\frac{1}{n}]$, $g>0$ in $A_n$ and $g<0$ in $B_n$.
Now let's create the measures $\mu_\theta$: for $\theta\leq k$ we can choose any measures we want. For $\theta>k+\frac{1}{n}$, consider for each $s,t>0$ the density functions $f_{\theta,s,t}=s\chi_{A_n}+t\chi_{B_n}+\chi_{[0,\theta]\setminus(A_n\cup B_n)}$.
Then $\int_{0}^{\theta} g(z) f_{\theta,s,t}(z)dz=\int_{[0,\theta]\setminus(A_n\cup B_n)} g(z)dz+s\int_{A_n}g(z)dz+t\int_{B_n}g(z)dz$. As $\int_{A_n}g(z)dz>0$ and $\int_{B_n}g(z)dz<0$, you can adjust $s,t$ so that $\int_{0}^{\theta} g(z) f_{\theta,s,t}(z)dz=0$.