We study the Schlesinger system of partial differential equations in the case when the unknown matrices of arbitrary size $(ptimes p)$ are triangular and the eigenvalues of each matrix form an arithmetic progression with a rational difference $q$, the same for all matrices. We show that such a system possesses a family of solutions expressed via periods of meromorphic differentials on the Riemann surfaces of superelliptic curves. We determine the values of the difference $q$, for which our solutions lead to explicit polynomial or rational solutions of the Schlesinger system. As an application of the $(2times2)$-case, we obtain explicit sequences of rational solutions and one-parameter families of rational solutions of Painleve VI equations. Using similar methods, we provide algebraic solutions of particular Garnier systems.